お世話になります
DotinstallでNode.jsを勉強中です
15章 https://dotinstall.com/lessons/basic_nodejs/26215の部分ですが
下記のようなエラーが出ます
よく理解できずに困っています
ご指導頂きたいです
[vagrant@localhost nodejs]$ node mongo.js
connected to db
/home/vagrant/nodejs/node_modules/mongodb/lib/mongo_client.js:797
throw err;
^
TypeError: db.collection is not a function
at /home/vagrant/nodejs/mongo.js:6:8
at args.push (/home/vagrant/nodejs/node_modules/mongodb/lib/utils.js:431:72)
at /home/vagrant/nodejs/node_modules/mongodb/lib/mongo_client.js:254:5
at connectCallback (/home/vagrant/nodejs/node_modules/mongodb/lib/mongo_client.js:933:5)
at /home/vagrant/nodejs/node_modules/mongodb/lib/mongo_client.js:794:11
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
[vagrant@localhost nodejs]$ throw err;
(settings.js)
exports.port = 1337;
exports.host = '192.168.33.10';
exports.db = 'nodedb';
(mongo.js)
var MongoClient = require('mongodb').MongoClient, settings = require('./settings'); MongoClient.connect("mongodb://localhost/"+settings.db, function(err, db) { if (err) { return console.dir(err); } console.log("connected to db"); db.collection("users", function(err, collection) { var docs = [ {name: "taguchi", score: 40}, {name: "fkoji", score: 80}, {name: "dotinstall", score: 60} ]; collection.insert(docs, function(err, result) { console.dir(result); }); }); }); コード
(server.js)
var http = require('http'), fs = require('fs'), ejs = require('ejs'), qs = require('querystring'); var settings = require('./settings'); var server = http.createServer(); var template = fs.readFileSync(__dirname + '/public_html/bbs.ejs', 'utf-8'); var posts = []; function renderForm(posts, res) { var data = ejs.render(template, { posts: posts }); res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data); res.end(); } server.on('request', function(req, res) { if (req.method === 'POST') { req.data = ""; req.on("data", function(chunk) { req.data += chunk; }); req.on("end", function() { var query = qs.parse(req.data); posts.push(query.name); renderForm(posts, res); }); } else{ renderForm(posts, res); } }); server.listen(settings.port, settings.host); console.log("server listening ..."); コード
このようになっています。
よろしくお願いいたします
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/13 00:03