本文介绍了Mongodb + express:typeerror:dbcollection不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试按照本教程将表单数据发送到mongodb:
[]
这是他连接到mongodb的方式:
var dbConn = mongodb .MongoClient.connect('mongodb:// localhost:27017');
这是他后面的代码所做的:
app.post('/ post-feedback',function(req,res){
dbConn.then(function(db){
delete req .body._id; //出于安全原因
db.collection('feedbacks')。insertOne(req.body);
});
res.send('收到的数据:\\ \\ n'+ JSON.stringify(req.body));
});
给出以下错误:
TypeError:dbcollection不是函数
似乎因为我使用mongo的3.6版本,我需要以另一种方式连接以使代码工作。但是怎么样?
我尝试了什么:
我试过像这样连接:
var dbConn = MongoClient.connect('mongodb:// localhost',function(err,client){
if(err)throw err ;
var db = client.db('mytestingdb');
});
但这只是给了我另一个错误(虽然在代码的不同部分):
TypeError:无法读取未定义的属性
解决方案
I'm trying to follow this tutorial to send form data to mongodb:
Save form data to MongoDB with NodeJS · programming mentor[^]
This is how he connects to mongodb:
var dbConn = mongodb.MongoClient.connect('mongodb://localhost:27017');
This is what he does later in the code:
app.post('/post-feedback', function (req, res) { dbConn.then(function(db) { delete req.body._id; // for safety reasons db.collection('feedbacks').insertOne(req.body); }); res.send('Data received:\n' + JSON.stringify(req.body)); });
which gives the following error:
TypeError: dbcollection is not a function
It seems that since I'm using version 3.6 of mongo, I need to connect in another way for the code to work. But how?
What I have tried:
I tried to connect like this:
var dbConn = MongoClient.connect('mongodb://localhost', function (err, client) { if (err) throw err; var db = client.db('mytestingdb'); });
But that just gives me another error (though in a different part of the code):
TypeError: Cannot read property then of undefined
解决方案
这篇关于Mongodb + express:typeerror:dbcollection不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!