2016-07-05 15 views
-1

データベース内のユーザー入力と一致する電子メールアドレスを照会しようとしています。私はfindOne()を使用していますが、どういうわけか、私はそれで問題が生じています: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 
+2

'findOne()'コールバック関数に 'err'パラメータがなく、結果(' matchinguser' param)のみがあります。 – chridam

+1

* "どういうわけか私はそれに問題を抱えています:" *拡張してください。 "Profile.findOne({emailaddress:req.body.emailaddress}、function(err、matchinguser){..' "問題" –

答えて

0

私はそれが立つようなコードを持ついくつかの問題があると思います。コールバックの欠落した "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を使用するように注意して、ちょうど私のthatsでしょう。

関連する問題