2016-11-21 6 views
1

私は多くを試してみましたが、私にはうってつけのものはありませんでした。 私はpromise.allを試して、配列をグローバルに設定しましたが、成功しませんでした。約束でオブジェクト配列を送信する方法

私はMongoDB上の3つのコレクションを検索したいと思います。そして、findが一致したら、オブジェクトをinfosで設定して配列にプッシュします。最後に、オブジェクト配列の応答を送信します。愚かな間違いのため申し訳ありませんが、私はコミュニティに頼ることにしました解決策を見つけようと多くの時間を費やしてきた

router.post('/certificado', (req, res) => { 
    let cpf = splita(req.body.cpf) 
    let array = [] 

    function pesquisaProjeto(cpf) { 
    return new Promise(function (fulfill, reject) { 
     ProjetoSchema.find({'integrantes.cpf':cpf}, 'integrantes.$ nomeProjeto -_id',(err, usr) => { 
     if (err) return reject(err) 
     fulfill(usr) 
     }); 
    }) 
    } 

    function pesquisaAvaliador(cpf) { 
    return new Promise(function (fulfill, reject) { 
     avaliadorSchema.find({'cpf':cpf}, 'nome -_id',(err, usr) => { 
     if (err) return reject(err) 
     fulfill(usr) 
     }) 
    }) 
    } 

    function pesquisaParticipante(cpf) { 
    return new Promise(function (fulfill, reject) { 
     participanteSchema.find({'cpf':cpf}, 'nome eventos -_id', (err, usr) => { 
     if (err) return reject(err) 
     fulfill(usr) 
     }) 
    }) 
    } 

    pesquisaProjeto(cpf) 
    .then(usr => { 
    let participante = ({ 
     tipo: usr[0].integrantes[0].tipo, 
     nome: usr[0].integrantes[0].nome 
    }) 
    array.push(participante) 
    console.log(participante) 
    }) 
    .catch(err => console.log("Não encontrou nada nos projetos. " + err.message)) 

    pesquisaAvaliador(cpf) 
    .then(usr => { 
    let participante = { 
     tipo: "Avaliador", 
     nome: usr[0].nome 
    } 
    array.push(participante) 
    console.log(array) 
    }) 
    .catch(err => console.log("Não encontrou nada nos avaliadores. " + err.message)) 

    pesquisaParticipante(cpf) 
    .then(usr => { 
    let participante = ({ 
     tipo: "Participante", 
     nome: usr[0].nome, 
     eventos: usr[0].eventos 
    }) 
    array.push(participante) 
    console.log(array) 
    }) 
    .catch(err => console.log("Não encontrou nada nos participantes dos eventos. " + err.message)) 

    **Anything like res.send(array) that I was tired to try** 
}); 

ありがとうございます!

+0

のためだけ短いのですか? – hyades

+0

わからない。 'promise.all'の' .then() 'で' res.send(array) 'しようとすると、何も前面に送られません。約束と配列宣言の中のプッシュでは何かが間違っているかもしれないと考えてください。 –

+0

「Promise.all」の使い方を教えてください。 – Bergi

答えて

1

私はあなたの質問を正しく理解していれば、複数の約束事があり、それらのすべてが終了するのを待っていたいと思います。 Promise.all()でそれを行うことができます。

Promiseが失敗すると、Promise.all()も失敗します。しかし、あなたがあなたの例のようにそれらをキャッチして何も返さない場合、私は配列がそのクエリのために未定義で生成されるべきだと思います。だから、あなたが望むならば、それらの空の値をフィルタリングすることができます。

const one = dbQueryOne.then(usr => ({ 
    key: usr.val 
})) 
.catch(err => { console.log(err) }) 

const two = dbQueryTwo.then(usr => ({ 
    key: usr.val 
})) 
.catch(err => { console.log(err) }) 

const three = dbQueryThree.then(usr => ({ 
    key: usr.val 
})) 
.catch(err => { console.log(err) }) 

Promise.all([one, two, three]).then(arr => { 
    res.send(arr.filter(val => val !== undefined)) 
}) 

usr => ({ key: val }) `promise.all`仕事しませんでした理由usr => { return { key: val } }

+0

ありがとうございました!これは本当に問題を解決しました! –

関連する問題