Node js problem when picking function value

Asked

Viewed 319 times

1

I have the following structure in Node :

module.exports.find = function(pesquisa,frase){ 
    return new Promise((resolve,reject) => {
        pesquisa.find(frase, function(err,data){
            if(err)
                throw err;
            else
                resolve(data[0]['retorno']);
        });
    });
}

I’d like to do the search.find return a value to some variable where I could manipulate, or something like that.

  • Rods, I think this question is duplicated from this https://answall.com/q/140814/129. Take a look there.

  • I had seen but I could not get the correct answer, could you help me taking as an example my function? Got a little confused in the options of the other question

  • Where do you want to use dados? Can you give an example? Can you use callbacks? can you use Promises? can you use async/await? Sorry to bombard with questions, but it’s easier that way.

  • This function is being called another page, I want to return the data to her , then if I have with the variable I can return it. I tried with Promisses but it appears pending. Ai i tried user o . then after promisse function and falls into the same problem.

  • Put that code with the trial code, I can explain what failed

1 answer

2


To use this file you can do so in the file that "requires":

const {find} = require('./nome-do-ficheiro-da-pergunta.js');

// não sei de onde vem pesquisa, mas assumo que tu sabes
find(pesquisa, 'teste').then(data => {
    // aqui dentro podes chamar a função que precisa de "data"
    // por exemplo
    res.send(data);
}).catch(e => console.log('Erro no find...', e);
  • That is, the secret was to treat the promisse on the page that called her, then. Thank you, I made the necessary changes and it worked

Browser other questions tagged

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