5

Can I do a synchronous find() with Sequelize? e.g.

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

(This seems to just get a promise.)

Noah
  • 4,601
  • 9
  • 39
  • 52

3 Answers3

7

I ended up doing this

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

It seems to work as expected.

Noah
  • 4,601
  • 9
  • 39
  • 52
  • 1
    In my case yield return Generator { suspended }, What is the way to enable this feature with Node.js 4? :) – jdnichollsc Oct 10 '16 at 19:19
  • 1
    Agree with @JuanDavid, I would love to know the answer to this. – NobleUplift Aug 25 '17 at 16:02
  • 1
    @NobleUplift I was using Koa with generators I think. You should look up async/await where this is pretty easy and no generators or koa required. But I don't think Node 4 works. – Noah Aug 25 '17 at 23:41
-1

Sequelize its all based in promises , if you are facing a use case like auth , you probably want to add a layer to your application , like middleware in express , so before your application execute some action , you need to authenticate users ( auth middleware ) and then pass to the next function.

//Auth Middleware example
function(req,res,next){
  User.find({id:1)
  .then(function(user){
     req.user = user;
     next();
  })
  .catch(function(error){
     next(error)
   })
}
cshion
  • 1,173
  • 1
  • 10
  • 19
-3

I'm recommend using await syntax

const user = await User.findById(id);
Hax0r
  • 1,722
  • 4
  • 25
  • 43