I need to associate variable to a key of the NODEJS object

Asked

Viewed 59 times

-1

I am finding in the database the correct seller, however when I try to associate the sellerSupport variable to the value of the support object within the user, it Undefined me and I cannot associate the value.

    let sellerSupport;  
    User.findOne({fullName: seller}, function(err, user) {
        sellerSupport = user.support;
    })

Someone would know how to fix it?

1 answer

0

You can use Sequelize asynchronously.

Example 1:

async function userController(req, res) {
  //Meu código

  const userData = await User.findOne({fullName: seller});

  //Desestruturação
  const { support: sellerSupport } = userData;

  //Minha lógica...

};

Or in a simpler way

Example 2:

async function userController2(req, res) {
  //Meu código

  const userData = User.findOne({fullName: seller});

  const sellerSupport = userData.support;

  //Minha lógica...

};

It is interesting to search for content on callback and asynchrony. A hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.