MongoDB,Express - 等待承诺,承诺{<pending>}

时间:2017-06-20 18:19:35

标签: javascript node.js mongodb express

我现在能够接收数据,因为mongo正在向我许诺

这是我的数据

{ "_id" : ObjectId("59490a250f86a4b4e0cb75b2"), "title" : "a", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b3") } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b3"), "title" : "b", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b4") } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b4"), "title" : "c", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b5") }, { "_id" : ObjectId("59490a250f86a4b4e0cb75b6") } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b5"), "title" : "d" }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b6"), "title" : "e", "array" : [ { "_id" : ObjectId("59490a250f86a4b4e0cb75b7") } ] }
{ "_id" : ObjectId("59490a250f86a4b4e0cb75b7"), "title" : "f" }

这是我在Mongo Shell中的MongoDb查询

function insertNodes(document) {

    var array = document.array;

    if (array) {
        array.forEach((innerDoc) => {
            let foreignKey = {_id: innerDoc._id};
            let foreignNode = db.test3.findOne(foreignKey);

            array[array.indexOf(innerDoc)] = insertNodes(foreignNode);
        });
    }

    return document;
}


var documents = db.test3.find();

documents.forEach((document) => {
    document = insertNodes(document);

    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    printjson(document);
});

但现在我想在serwer上做这个查询(因为我想从db接收数据)

所以我在控制器中重构这个

function insertNodes(document) {

    var array = document.array;

    if (array) {
        array.forEach((innerDoc) => {
            var foreignKey = {_id: innerDoc._id};
            var foreignNode = db.collection('test3').findOne(foreignKey);
            foreignNode.then(function (data) {
                console.log(data);
                foreignNode = data;
            });

            console.log(foreignNode);

            array[array.indexOf(innerDoc)] = insertNodes(foreignNode);
        });
    }

    return document;
}


var documents = db.collection('test3').find();

var docs = [];

documents.forEach((document) => {
    docs.push(insertNodes(document));
});

res.send(docs);

但这一行

var foreignNode = db.collection('test3').findOne(foreignKey);

让我回来

Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }

我添加“.then”后承诺我仍然会收到承诺,但我无法做任何事情。

1 个答案:

答案 0 :(得分:0)

所以,我想问题是我正在使用forEach和map来接收承诺

https://github.com/babel/babel/issues/909

Using async/await with a forEach loop

这是我的解决方案:

db.collection('test3').find().toArray(async function (err, collection) {

    async function insertNodes(document) {

        var array = document.array;

        if (array) {

            for (let innerDoc of array) {
                array[document.array.indexOf(innerDoc)] = await insertNodes(await db.collection('test3').findOne({_id: innerDoc._id}));
            }
        }
        return document;
    }

    var fsdf = [];
    for (let c of collection) {
        fsdf.push(await insertNodes(c))
    }
    res.send(fsdf)

});