0

Build an application with MongoDB and NodeJS as backend-server. Fetched some companies details from database according to their category id, the problem is that the information is not displayed on both postman and ionic project, when called this API.

What to do to resolve this problem?

Sample code:

function filtrarPorCategoria(busqueda, regex) {
    return new Promise((resolve, reject) => {
        Empresa.find({ categoria: busqueda }, 'nombre estado').exec((err, empresas) => {
            if (err) {
                reject('Error al cargar las empresas', err);
            } else {
                console.log(empresas);
                resolve(empresas);
            }
        });
    });
}

This is response over postman

NAVIN
  • 3,193
  • 4
  • 19
  • 32
rolex92
  • 45
  • 5

1 Answers1

0

You do not need to wrap the whole thing with Promise since exec gives you a fully-fledged one already:

const getFn = (busqueda) =>
    Empresa.find({ categoria: busqueda }, 'nombre estado').exec()
      .then(empresas => console.log(empresas))
      .catch(err => console.log(err))  

Also in your case specifically you are dealing with promise and you are not returning anything (should be return Empresa.find(...). In the code above we use ES6 implicit return.

Akrion
  • 18,117
  • 1
  • 34
  • 54
  • You might have to add more context (pieces of your code). Like try `console.log(empresas)` right before the `if` and see what is in there etc. – Akrion Sep 29 '18 at 00:52