1

I'm now able to receive data because mongo is returning promise to me

This is my data

{ "_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" }

And this is my MongoDb query in Mongo Shell

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

but now I would like to do this query on serwer (because I would like to receive data from db)

so I refactor to this in controller

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

but this line

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

is returnning me

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

after I add ".then" to promise I still receive promise but I cant do anything with this.

Oskar Woźniak
  • 715
  • 2
  • 10
  • 25
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – 4castle Jun 20 '17 at 18:23
  • @4castle I dont think this is the same because I used "then" – Oskar Woźniak Jun 20 '17 at 18:53
  • I linked to the canonical duplicate for the problem you're experiencing, but the first answer links to [this question](https://stackoverflow.com/q/23667086/5743988) which shows how your usage of `then` results in the same outcome as not using promises at all. – 4castle Jun 20 '17 at 18:56

1 Answers1

1

so, I guess the problem was that I was using forEach and map to receive promise

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

Using async/await with a forEach loop

this is my solution:

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)

});
Oskar Woźniak
  • 715
  • 2
  • 10
  • 25