简体   繁体   English

NodeJS和MongoDB中的过滤器出现问题

[英]problem with filter in NodeJS and MongoDB

Build an application with MongoDB and NodeJS as backend-server. 使用MongoDB和NodeJS作为后端服务器构建应用程序。 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. 根据category id从数据库中获取了一些companies详细信息,问题是,在调用此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 这是对邮递员的回应

You do not need to wrap the whole thing with Promise since exec gives you a fully-fledged one already: 您不需要用Promise来包装整个东西,因为exec已经为您提供了成熟的东西:

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. 同样在您的情况下,您正在处理promise,并且不返回任何内容(应return Empresa.find(... )。在上面的代码中,我们使用ES6隐式返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM