I have a function,
var findUserDevice = function(userDeviceId){ var device = db.DeviceUser.find({ where: { id: userDeviceId } }).then(function(device) { if (!device) { return 'not find'; } return device.dataValues; }); };
but this function does not return anything...
var UserDevice = findUserDevice(req.body.deviceUserId); console.log(UserDevice);// undefined
If you want to do the search based on id, should go for findById() function of your model. Save this answer.
Sequelize uses an option called returning to specify which data returns from a commands like . destroy() or update() . Let's look at three common Sequelize commands and see how to handle the data returned by each.
findByPk. Returns the row with the given value of Primary Key.
The operation you are trying to do is async
, which means that you need to use a callback. Since sequelize
is build on top of Promises, you should actually write your code like this :
var findUserDevice = function(userDeviceId){ // return the promise itself return db.DeviceUser.find({ where: { id: userDeviceId } }).then(function(device) { if (!device) { return 'not find'; } return device.dataValues; }); };
And later use it like :
findUserDevice(req.body.deviceUserId).then( function(UserDevice) { console.log(UserDevice); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With