从javascript中获取回调数据?

时间:2017-08-06 14:32:13

标签: javascript graphql

我想知道将令牌变量恢复到更高阶函数范围的最佳方法>>所以我可以从高阶函数返回它

signinUser: async (_, data) => {
  const user = await User.findOne({'email': data.email.email}).exec();

  console.log(bcrypt.compare(data.email.password,user.password, (err,result)=>{
    if(err){return("sorry error")}
    if(result) {
      var token = jwt.sign({_id: user._id},
                           config.secret, {expiresIn: 60 * 60 * 24 * 7});
    }
  }))

  return {user: {id: user._id, name: user.name, email: user.email}, token: "this is a token"}
}

1 个答案:

答案 0 :(得分:0)

如果你想继续使用async await语法,你需要将bcrypt.compare调用传递给带有Q或BlueBird库的promise,并编写如下代码:

let result = await Q.nfcall(bcrypt.compare, data.email.password,user.password)
if(result) {
    var token = jwt.sign({_id: user._id},
                       config.secret, {expiresIn: 60 * 60 * 24 * 7});
}

return {id: user._id, name: user.name, email: user.email}, token: token}