Sequelize 同步查找

Sequelize synchronous find

我可以使用 Sequelize 进行同步 find() 吗?例如

const user = User.find({ id: 1 })

(这好像刚得到一个承诺。)

Sequelize its all based on promises ,如果你面对像 auth 这样的用例,你可能想在你的应用程序中添加一个层,比如 express 中的中间件,所以在你的应用程序执行某些操作之前,你需要进行身份验证users ( auth middleware ) 然后传递给下一个函数。

//Auth Middleware example
function(req,res,next){
  User.find({id:1)
  .then(function(user){
     req.user = user;
     next();
  })
  .catch(function(error){
     next(error)
   })
}

我最后做了这个

const user = yield User.find({ id });

它似乎按预期工作。

我建议使用 await 语法

const user = await User.findById(id);