Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何正确获取承诺返回的值?_Javascript_Node.js_Express_Promise - Fatal编程技术网

Javascript 如何正确获取承诺返回的值?

Javascript 如何正确获取承诺返回的值?,javascript,node.js,express,promise,Javascript,Node.js,Express,Promise,我正在使用nodejs+Express作为我的后端服务 我有一个authenHandler.js文件来帮助使用sequelize进行身份验证: module.exports = { isAuthenticated: function(data) { models.Users.find(data) .then(function(user) { if (user) { return true; } else {

我正在使用nodejs+Express作为我的后端服务

我有一个authenHandler.js文件来帮助使用sequelize进行身份验证:

module.exports = {
  isAuthenticated: function(data) {
    models.Users.find(data)
      .then(function(user) {
        if (user) {
          return true;
        } else {
          return false;
        }
      });
  }
}
当我在app.js中使用此helper函数时:

app.use(function(req, res, next) {
        // process to retrieve data
        var isAuthenticated = authProvider.isAuthenticated(data);
        console.log(isAuthenticated);
        if (isAuthenticated) {
            console.log("auth passed.");
            next();
        } else {
            var err = new Error(authenticationException);
            err.status = 403;
            next(err);
        }
    }
})
这总是转到else语句,因为isAuthenticated打印行总是返回undefined。看起来承诺在调用if-else语句后返回了值


我不确定如何连接authenHandler.js和app.js。最好的方法是什么?

更改它以回报承诺

isAuthenticated: function(data) {
    return models.Users.find(data)
      .then(function(user) {
        if (user) {
          return true;
        } else {
          return false;
        }
      });
  }
然后兑现承诺

authProvider.isAuthenticated(data)
.then((result =>{
var isAuthenticated = result;
  console.log(isAuthenticated);
        if (isAuthenticated) {
            console.log("auth passed.");
            next();
        } else {
            var err = new Error(authenticationException);
            err.status = 403;
            next(err);
        }
}))

你的app.js错误,已验证返回承诺不返回bool

你需要像这样修改app.js

app.use(function(req, res, next) {

        // process to retrieve data
        authProvider.isAuthenticated(data)
          .then(function(isAuthenticated) {
            if (isAuthenticated) {
              console.log("auth passed.");
              next();
            }
            else {
              var err = new Error(authenticationException);
              err.status = 403;
              next(err);
            }
          });
    }
})

你使用require('authenHandler.js')了吗?请参阅副本中的“Promise gotchas”。通过在isAuthenticated函数中保留相同的参数req、res和next,你可以将isAuthenticated函数用作中间件,并可以处理以在函数中检索日期本身。看起来我还需要更改authProvider.isAuthenticated()让它回报你的承诺?我该怎么做?@jamesdeath123您应该在models.Users.xxxxx之前添加“return”如果在isAuthenticated函数中,我还有一个检查:如果(!data){return false;}在我调用sequelize承诺之前?我应该如何将数据验证包装成承诺?谢谢那么result=>{}到底是什么意思呢?我认为它在nodejs中不是有效的语法?()=>{}是es6 arrow函数,在旧版本中可以写成'function(){}'。节点支持箭头函数。(请参阅版本兼容性)。我不明白你的第一个问题。