有些人可以帮助我理解为什么我不能在mongoDB中插入任何内容吗?
这是我的发布路径
router.post('/', function(req, res, next) {
console.log("i am posting data!");
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/blog';
MongoClient.connect(url, function(err, db) {
if (err) {
console.log('Error:', err);
} else {
console.log('connected to db');
var collection = db.collection('blogPost');
var blogPost = {
title: req.body.title,
status: req.body.status,
category: req.body.category,
tags: req.body.tags,
content: req.body.content,
createDate: new Date()
}
console.log("posting data:", blogPost);
collection.insert(blogPost, function(err, res) {
if (err) {
console.log('Error on Insert:', err);
} else {
console.log("insert successful");
}
db.close();
res.render('index', {
title: 'Welcome!!!',
pageName: 'index'
});
});
}
db.close();
});
});

以下是我在控制台日志中看到的内容。
GET /js/bootstrap-multiselect.js 200 16.638 ms - 66344
i am posting data!
connected to db
posting data: { title: 'test',
status: 'A',
category: 'excerpt',
tags: [ 'life', 'teaching' ],
content: 'Test from app',
createDate: Sun Sep 04 2016 23:46:18 GMT-0600 (MDT) }
insert successful
Sun Sep 04 2016 23:46:18 GMT-0600 (MDT): Node server stopped.
/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
^
TypeError: res.render is not a function
at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/routes/index.js:94:13
at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/collection.js:523:5
at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/collection.js:701:5
at handleCallback (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/utils.js:96:12)
at executeCommands (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/bulk/ordered.js:405:12)
at resultHandler (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/bulk/ordered.js:433:5)
at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:436:18
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)
Brians-MacBook-Pro:illcomplacent bberrelez$

问题是,它甚至没有插入MongoDB。我可以从命令行插入数据,但这不是我需要做的。我需要从应用程序中插入这些数据。
答案 0 :(得分:1)
您已将
res
作为insert callback function
中的参数,这就是您获取此内容的原因。且
local rest
没有render
功能。
希望这对你有用。
router.post('/', function(req, res, next) {
console.log("i am posting data!");
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/blog';
MongoClient.connect(url, function(err, db) {
if (err) {
console.log('Error:', err);
} else {
console.log('connected to db');
var collection = db.collection('blogPost');
var blogPost = {
title: req.body.title,
status: req.body.status,
category: req.body.category,
tags: req.body.tags,
content: req.body.content,
createDate: new Date()
}
console.log("posting data:", blogPost);
collection.insert(blogPost, function(err, result) {// change `res` with any other vaiable
if (err) {
console.log('Error on Insert:', err);
} else {
console.log("insert successful");
}
db.close();
res.render('index', {
title: 'Welcome!!!',
pageName: 'index'
});
});
}
db.close();
});
});
答案 1 :(得分:1)
您已使用相同的变量名称res
两次。当您到达res.render
时,res
本地作用于您的回调,并且它指的是您插入的结果,而不是您的响应对象。
要解决此问题,您可以简单地重命名插入结果。尝试更改:
collection.insert(blogPost, function(err, res) {
到
collection.insert(blogPost, function(err, result) {
这并不能解释为什么您的数据没有插入,但看起来您的数据应该在那里。您确定要查找正确的数据库(博客)和集合(blogPost)吗?
答案 2 :(得分:0)
我使用Django有类似的东西,其中驱动程序将我的集合名称转换为小写。可能是node.js正在做同样的事情。尝试检查Mongo shell,看看你的代码是否创建了一个blogpost集合并将文档插入到那里,而不是在blogPost中。