gpt4 book ai didi

Node.js 应用程序关闭 MongoError 连接

转载 作者:可可西里 更新时间:2023-11-01 10:15:39 26 4
gpt4 key购买 nike

当我尝试连接 node.js 应用程序时,我得到:

MongoError: connection 0 to 127.0.0.1:27017 closed

var MongoClient = require('mongodb').MongoClient
,format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if(err) throw err;
var collection = db.collection('student');
collection.insert({id:4,name:shyam}, function(err, docs) {
collection.count(function(err, count) {
console.log(format("count = %s", count));
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
console.dir(results);
// Let's close the db
db.close();
});
});
})

最佳答案

下次你应该写出更好的问题。

var mongo = require('mongoose');

mongo.connect('mongodb://localhost:27017/dbname');

// Connect to mongo
var db = mongo.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('Connected');
});

learn more about how to connect to MongoDB with node application Mongoose

关于Node.js 应用程序关闭 MongoError 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43824103/

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