Q
Q
qovalenko2019-05-01 14:50:39
MongoDB
qovalenko, 2019-05-01 14:50:39

How to pass the result of a query to another function?

The question is elementary, something caused difficulty.
First I find the parameter

db.collection("collection").findOne({colID: 1}, function(err, result) {
        if (err) throw err;
        console.log(result.title);
        client.close();
    });

Then I add this parameter to the collection
data = { title: result.title }
    db.collection('collectiontwo').insertOne(data, function(err, res) {
        if (err) throw err;
        console.log('1 document inserted');
        client.close();
    });

How to implement this moment synchronously (result.title) and without global variables.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2019-05-01
@qovalenko

const result = await db.collection("collection").findOne({colID: 1});
const res = await db.collection('collectiontwo').insertOne({title: result.title}); 
client.close();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question