Node.js 使用findOne()查询电子邮件地址

Node.js 使用findOne()查询电子邮件地址,node.js,mongodb,sails.js,Node.js,Mongodb,Sails.js,我正在尝试查询数据库中与用户输入匹配的电子邮件地址。我使用的是findOne(),但不知怎的,我遇到了一些问题: Profile.findOne({emailaddress : req.body.emailaddress}, function(matchinguser) { console.dir("matching user" + matchinguser); Profile.create(req.params.all(), function

我正在尝试查询数据库中与用户输入匹配的电子邮件地址。我使用的是
findOne()
,但不知怎的,我遇到了一些问题:

Profile.findOne({emailaddress : req.body.emailaddress},   function(matchinguser) {
            console.dir("matching user" + matchinguser);
            Profile.create(req.params.all(), function (err, profile) {
                console.dir(profile);
                if (err) {
                    req.session.flash = {
                        err: err
                    }
                    return res.redirect('/profile/new')
                }
                res.redirect('/profile')
                req.session.flash = {};
            }) //profile created
        }) //findone

我认为目前的代码存在一些问题。回调中缺少的“err”就是其中之一,但我认为您应该使用“exec”。现在,您的函数正在尝试执行,就像您在sails.js中格式化本机查询一样。以下是我推荐的调整代码:

Profile.findOne({emailaddress : req.body.emailaddress}).exec(err,function(matchinguser) {
     //Handle errors  
     if (err){ return res.negotiate(err);}

     //Handle success
     //I assume do some checks on the user found e.g. return them or if not found create the user
    if(!matchinguser){
     //No user found so redirect back
     return res.redirect('/profile');}
    //Rest of your code can go in here if needed I've just redirected you back for now.
    return res.redirect('/profile/);
    });
我也会注意在查询中直接使用req.body.emailaddress而不检查它,但那只是我。

您的
findOne()
回调函数缺少
err
参数,它只有结果(
matchinguser
param)。尝试添加它
Profile.findOne({emailaddress:req.body.emailaddress},function(err,matchinguser){..
,看看它是如何运行的。”但不知怎的,我遇到了一些问题:“在所说的“问题”上展开”