2016-08-04 5 views
1

エラー関数を使用してJSONエラーオブジェクトをコードに渡そうとしています。電子メールとパスワードチェックステートメントに1回、if existingUserステートメントにもう一度入力します。私はそれが夜のその時だと思う。JSON形式を返さないエラー関数

あなたが反応して右のステータスコードを返すされていない現時点では
const User = require('../models/user'); 

exports.signup = function(req, res, next) { 
    const email = req.body.email; 
    const password = req.body.password; 

    if (!email || !password) { 
     return res.err("Please enter in email and password"); 
    } 

    //See if a user with the given email exists 
    User.findOne({ email: email }, function(err, existingUser) { 
     if (err) { return next(err); } 

     //If a user with email does exist, return an Error 
     if (existingUser) { 
     //the status sets the status of the http code 422 means couldn't process this 
      return res.err('Email is in use'); 
     } 
     //If a user with email does NOT exist, create and save user record 
     const user = new User({ 
      email: email, 
      password: password 
     }); 

     user.save(function(err) { 
      if (err) { return next(err); } 
      //Respond to request indicating the user was created 
      res.json({ success: true }); 
     }); 
    }); 
} 
+0

これはどうやって行ったのですか? – alexi2

答えて

1

、あなたはこれを試みることができる:

置き換えます

return res.err("Please enter in email and password");

return res.status(422).send({error: "Please enter in email and password"})

return res.err('Email is in use');

で:と置き換える

return res.status(422).send({ error: "Email is in use" });

これは、HTTPレスポンスに必要なステータスコードを返送します。

一貫性を保つために、コード内に一重引用符または二重引用符を使用することも検討してください。

関連する問題