簡體   English   中英

NodeJS和MongoDB中的過濾器出現問題

[英]problem with filter in NodeJS and MongoDB

使用MongoDB和NodeJS作為后端服務器構建應用程序。 根據category id從數據庫中獲取了一些companies詳細信息,問題是,在調用此API時,該信息未同時顯示在郵遞員和離子項目上。

如何解決此問題?

樣例代碼:

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);
            }
        });
    });
}

這是對郵遞員的回應

您不需要用Promise來包裝整個東西,因為exec已經為您提供了成熟的東西:

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

同樣在您的情況下,您正在處理promise,並且不返回任何內容(應return Empresa.find(... )。在上面的代碼中,我們使用ES6隱式返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM