1

I'm trying to get information from my model but it always returns me Promise { <pending> } or undefined (on all the ways I had tried)

Heres the code that I'm trying to use to retrieve information from DB

const cnabBody = require('../controller/cnabBody');

let resultado = cnabBody.encontrarUm().then((r) => {
    console.log(r);
});

Heres my controller

const CnabBody = require ('../model/cnabBody');

exports.encontrarUm = async () => {
    const { nome_campo } = await CnabBody.findOne({where:{nome_campo: "Nome do Campo"}});
    return nome_campo;
}

1
  • Change const { nome_campo } = await CnabBody.findOne(...) to const nome_campo = await CnabBody.findOne(...). Commented Jan 22, 2020 at 3:00

3 Answers 3

1

I would need to know more about the object structure that's resolved from the findOne function, but it sounds like the nome_campo object being returned is a Promise object rather than a value. If that's the case then you'd also have to await on the nome_campo (assuming it's not undefined).

If CnabBody.findOne() returns this:

{
  nome_campo: somePromise 
}

then you should either change findOne to await on that Promise and send back the object it resolves to, or you need to await on it after receiving it in your controller. The latter could be done like this:

const CnabBody = require ('../model/cnabBody');

exports.encontrarUm = async () => {
  const { nome_campo } = await CnabBody.findOne({where:{nome_campo: "Nome do Campo"}});
  if (nome_campo) return await nome_campo;  // <--- add await here if defined
}

However I'd say it's nicer if findOne could be changed (assuming you have access to the code) so that calling await CnabBody.findOne() returned the actual result and not a Promise. Having Promise that resolves another Promise seems redundant, but if you are not the author of findOne then you might not have the option to change its resolved object.

Sign up to request clarification or add additional context in comments.

Comments

1

In your controller change const { nome_campo } to const nome_campo. it will work

const CnabBody = require ('../model/cnabBody');

exports.encontrarUm = async () => {
   // const { nome_campo } = await CnabBody.findOne({where:{nome_campo: "Nome do Campo"}}); <== problem is here
      const nome_campo = await CnabBody.findOne({where:{nome_campo: "Nome do Campo"}}); 

    return nome_campo;
}

Comments

0

I was calling my async functions inside a function that wasnt async soo when i tried any await method it wasnt awaiting or returning a error soo i changed my first line wb.xlsx.readFile(filePath).then(function(){ to wb.xlsx.readFile(filePath).then(async function(){

soo my code looks like this now and it is working fine. (:

wb.xlsx.readFile(filePath).then(async function(){
    var sh = wb.getWorksheet("Sheet1");
    // console.log(sh.getCell("A1").value);

    const field = await cnabContent.findOne({where: {nome_campo: "Nome do Campos"}});
    console.log(field);
});

Thanks for all that tried to help me, made me do alot of searchs and read about promises, async and await and get this solution.

2 Comments

how is this code working fine?? you have used await and not used async in your function?
Thanks for notification, i forgot to write the async but it is in my function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.