Node.js Mongoose不调用save()和find()的回调
我不会对save()或find()方法进行回调。它也没有进入mongoose.connect的回调。知道为什么吗 这是我的密码:Node.js Mongoose不调用save()和find()的回调,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我不会对save()或find()方法进行回调。它也没有进入mongoose.connect的回调。知道为什么吗 这是我的密码: mongoose.connect("mongodb://localhost/blah", { useMongoClient: true, }, function (err) { console.log('connect to database'); // Not called if (err) { throw err;
mongoose.connect("mongodb://localhost/blah", {
useMongoClient: true,
}, function (err) {
console.log('connect to database'); // Not called
if (err) {
throw err;
}
});
var Schema = mongoose.Schema
var User = new Schema({
author : String
, type : String
});
var MyUserModel = mongoose.model('User', User); //create and access the model User
var u = new MyUserModel();
u.author = 'authorname';
u.save(function(err){
console.log("saved") // not called
if (err) console.log(err);
});
MyUserModel.find({}, function (err,docs) {
console.log("found"); // not called
console.log(docs);
});
在做任何事情之前,您需要“等待”连接。它是一个“异步”函数,与所有异步函数一样,在继续其他依赖于结果的代码之前,需要等待它们的解析。在这种情况下,数据库的后续操作将需要“连接”
在现代环境中使用异步/await
可获得最佳效果:
// setup part
var Schema = mongoose.Schema
var User = new Schema({
author : String
, type : String
});
var MyUserModel = mongoose.model('User', User); //create and access the model User
// async parts - wrapped in a self executing closure with the async keyword
(async function() {
try {
const conn = await mongoose.connect("mongodb://localhost/blah", {
useMongoClient: true,
});
console.log('database connected');
// Add a user and wait for it to save
var u = new MyUserModel();
u.author = 'authorname';
await u.save();
console.log("saved");
// find the documents and await that too
let docs = await MyUserModel.find();
console.log(JSON.stringify(docs,undefined,2));
} catch(e) {
console.error(e);
} finally {
mongoose.disconnect();
}
})();
或者通过链接旧版本nodejs中的承诺:
// setup part
var Schema = mongoose.Schema
var User = new Schema({
author : String
, type : String
});
var MyUserModel = mongoose.model('User', User); //create and access the model User
// Async part
mongoose.connect("mongodb://localhost/blah", { useMongoClient: true })
.then( () => {
var u = new MyUserModel();
u.author = 'authorname';
return u.save();
})
.then( () => MyUserModel.find() )
.then( docs => {
console.log(JSON.stringify(docs,undefined,2));
mongoose.disconnect();
})
.catch( e => console.error(e) );
底线是您需要等待所有异步调用的完成,.connect()
现在是现代mongoose版本中的异步调用。它真的应该一直这样处理,但现在你“必须”这样处理。在做任何事情之前,你需要“等待”连接。它是一个“异步”函数,与所有异步函数一样,在继续其他依赖于结果的代码之前,需要等待它们的解析。在这种情况下,数据库的后续操作将需要“连接”
在现代环境中使用异步/await
可获得最佳效果:
// setup part
var Schema = mongoose.Schema
var User = new Schema({
author : String
, type : String
});
var MyUserModel = mongoose.model('User', User); //create and access the model User
// async parts - wrapped in a self executing closure with the async keyword
(async function() {
try {
const conn = await mongoose.connect("mongodb://localhost/blah", {
useMongoClient: true,
});
console.log('database connected');
// Add a user and wait for it to save
var u = new MyUserModel();
u.author = 'authorname';
await u.save();
console.log("saved");
// find the documents and await that too
let docs = await MyUserModel.find();
console.log(JSON.stringify(docs,undefined,2));
} catch(e) {
console.error(e);
} finally {
mongoose.disconnect();
}
})();
或者通过链接旧版本nodejs中的承诺:
// setup part
var Schema = mongoose.Schema
var User = new Schema({
author : String
, type : String
});
var MyUserModel = mongoose.model('User', User); //create and access the model User
// Async part
mongoose.connect("mongodb://localhost/blah", { useMongoClient: true })
.then( () => {
var u = new MyUserModel();
u.author = 'authorname';
return u.save();
})
.then( () => MyUserModel.find() )
.then( docs => {
console.log(JSON.stringify(docs,undefined,2));
mongoose.disconnect();
})
.catch( e => console.error(e) );
底线是您需要等待所有异步调用的完成,
.connect()
现在是现代mongoose版本中的异步调用。它确实应该一直这样处理,但现在您“必须”这样处理。您成功连接到数据库了吗?这一行?猫鼬。连接(“mongodb://localhost/blah“,…?@MikeMiller添加连接到数据库并使用集合的部分。显示.connect()的完整代码
在您的问题中。很可能您的connect仍在等待承诺解决方案。Mongoose在最近几个月改变了处理方式。您基本上应该做.connect(…)。然后(…)
或者更好的等待Mongoose.connect(…)
。不要使用useMongoClient:true
是否成功连接到数据库?此行?mongoose.connect(“mongodb://localhost/blah“,…?@MikeMiller添加连接到数据库并使用集合的部分。显示.connect()的完整代码
在您的问题中。很可能您的connect仍在等待承诺解决方案。Mongoose在最近几个月改变了处理方式。您基本上应该做.connect(…)。然后(…)
或者更好的等待Mongoose.connect(…)
。不要使用useMongoClient:true
我复制了您的两个代码示例,在这两种情况下,它在连接调用期间仍然挂起,并且从不打印“数据库已连接”(或进入“then”部分)。你知道为什么吗?如果我的配置对你有效,但对我无效,那么我的配置肯定有问题。节点:v8.4.0,Mongood db版本3.4.7,mongoose:4.11.10,express:4.15.4。@MikeMiller创建一个新的目录文件夹,只安装mongoose和这里的列表。你可能安装失败了。但这就是自包含的列表或者。因此,你应该像那样运行此代码,而不是将其放入现有的项目目录中。的确,这很有效。我想知道我在安装过程中搞砸了什么。好吧,现在我有一些事情要做。非常感谢!我复制了你的两个代码示例,在这两种情况下,它在连接调用期间仍然挂起,并且从未打印出来“数据库已连接”(或进入“then”部分)。你知道为什么吗?如果我的配置对你有效,但对我无效,那么我的配置肯定有问题。节点:v8.4.0,Mongood db版本3.4.7,mongoose:4.11.10,express:4.15.4。@MikeMiller创建一个新的目录文件夹,只安装mongoose和这里的列表。你可能安装失败了。但这就是自包含的列表或者。因此,你应该像那样运行此代码,而不是将其放入现有的项目目录中。确实,这很有效。我想知道我在安装过程中把什么搞砸了。好吧,现在我有一些东西要处理。非常感谢!