gpt4 book ai didi

Unable to insert record in MongoDB(无法在MongoDB中插入记录)

转载 作者:bug小助手 更新时间:2023-10-25 11:19:08 25 4
gpt4 key购买 nike



Can some help me understand why I cannot insert anything into my mongoDB?

能帮我解释一下为什么我不能在MongoDB中插入任何东西吗?



Here is my posting route

这是我的邮寄路线





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();

});

});





Here is what I am seeing in the console log.

这是我在控制台日志中看到的内容。





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$





The thing is, it's not even inserting into the MongoDB. I can insert data just fine from the command line but, that is not what I am needing to do. I need to insert this data from the application.

问题是,它甚至没有插入到MongoDB中。我可以很好地从命令行插入数据,但这不是我需要做的。我需要从应用程序中插入此数据。


更多回答

replace res in collection.insert(blogPost, function(err, res) {} ) with any other variable name which is not use . It is overwriting above router.post('/', function(req, res, next) {} variable

用任何其他不使用的变量名替换Collection tion.Insert(Blopost,Function(err,res){})中的res。正在覆盖router.post(‘/’,Function(req,res,Next){}变量

优秀答案推荐

You've used the same variable name, res, twice. By the time you get to res.render, res is locally scoped to your callback and it's referring to the result from you insert, not your response object.

您已经使用了相同的变量名res两次。当您到达res.render时,res的作用域是本地的回调,并且它引用的是来自插入的结果,而不是响应对象。



To solve that you can simple rename your insert result. Try changing:

要解决这个问题,您可以简单地重命名您的插入结果。尝试更改:



collection.insert(blogPost, function(err, res) {


to



collection.insert(blogPost, function(err, result) {


That doesn't explain why your data isn't inserted, but it looks like your data should be there. Are you sure you're looking in the right database (blog) and collection (blogPost)?

这并不能解释为什么不插入您的数据,但看起来您的数据应该在那里。你确定你找的是正确的数据库(博客)和集合(博客)吗?




You have given res as the parameter in insert callback function that's why you are getting this.


And local rest don't have render function.



Hope this will work for you.

希望这对你有用。


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();
});
});


I had something similar using Django where the driver converted my collection name to be all lower case. It may be that node.js is doing the same. Try checking in the Mongo shell to see if your code has created a blogpost collection and inserted the documents there instead of in blogPost.

我使用Django时也有类似的情况,其中驱动程序将我的收藏名称全部转换为小写。也许node.js也在做同样的事情。尝试签入Mongo外壳,以查看您的代码是否创建了一个Blopost集合并将文档插入其中,而不是插入到Blopost中。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com