學(xué)習(xí)使用Node.js連接MongoDB數(shù)據(jù)庫梗搅,剛開始的代碼如下:
mongoose.connect('mongodb://localhost:27017/blog' ,,function(err){
if(err){
console.log('數(shù)據(jù)庫連接失敗');
}else{
console.log('數(shù)據(jù)庫連接成功');
app.listen(8090);
}
});
1.出現(xiàn)問題
運(yùn)行時(shí)出現(xiàn)錯(cuò)誤:
image.png
2. 分析問題
根據(jù)錯(cuò)誤日志,這個(gè)parser有點(diǎn)過時(shí)了椿猎,我們應(yīng)該使用新的方法把{ useNewUrlParser: true }添加到MongoClient.connect的參數(shù)中食棕。
3.解決問題
更改過后:
mongoose.connect('mongodb://localhost:27017/blog' ,{ useNewUrlParser: true },function(err){
if(err){
console.log('數(shù)據(jù)庫連接失敗');
}else{
console.log('數(shù)據(jù)庫連接成功');
app.listen(8090);
}
});