mongoose findOne({email:email})经常很少工作不起作用?

时间:2015-10-26 18:53:52

标签: node.js mongodb mongoose

我使用Mongoose 4.1.11

执行此错误非常困难:

console.log("step1"); // that's display every time in my console => ok

models.users.findOne({email: mail}, function (err, myUser) {
    console.log("step2"); // That's very very weird, that's work but RARELY that's doesn't work
    ...

有时,由于此问题,我的API服务器无效,

编辑#1

所以,我将服务器托管从Gandi更改为Heroku。现在,它的作品非常好!谢谢你的帮助; - )

3 个答案:

答案 0 :(得分:2)

您应该使用错误回调参数,如下所示;

models.users.findOne({email: mail}, function (err, myUser) {
    if (!err) console.log("step2"); // That's very very weird,that's work but RARELY that's doesn't work
    else console.log(err.message);
    ...

Mongo会在出现问题时生成错误对象,您可能会在此用法中出现错误原因。

答案 1 :(得分:2)

    var User = mongoose.model('User', UserSchema);
    User.findOne({email: mail}, function (err, myUser) {
        console.log("step2"); // That's very very weird, that's work but RARELY that's doesn't work
    }

答案 2 :(得分:0)

所以,我将服务器托管从Gandi更改为Heroku。现在,它的作品非常好!谢谢你的帮助; - )