How to transfer query result to another function?

The question is simple, what caused the problem.
First, I find the option
db.collection("collection").findOne({colID: 1}, function(err, result) {
 if (err) throw err;
console.log(result.title);
client.close();
 });

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

How to implement this time synchronously (result.title) and without global variables.
Thank you!
March 20th 20 at 11:22
1 answer
March 20th 20 at 11:24
Solution
const result = await db.collection("collection").findOne({colID: 1});
const res = await db.collection('collectiontwo').insertOne({title: result.title}); 
client.close();
Still as a variant array:
(async () => {
 for (const id of array) {
 const result = await db.collection("collection").findOne({colID: 1});
 const res = await db.collection('collectiontwo').insertOne({title: result.title}); 
 // ...
}
client.close();
})();
- angelina38 commented on March 20th 20 at 11:51
the result brought a Promise { }? - Margarett_Kunde commented on March 20th 20 at 11:27
@maryjane.Yundt, without await'and brought to Promise - cornell_Goyette commented on March 20th 20 at 11:30
@dayne6With led SyntaxError await: await is only valid in async function - Margarett_Kunde commented on March 20th 20 at 11:33
@maryjane.Yundt, wrap in asynchronous function
(async () => {
 const result = await db.collection("collection").findOne({colID: 1});
 const res = await db.collection('collectiontwo').insertOne({title: result.title}); 
client.close();
})();
- cornell_Goyette commented on March 20th 20 at 11:36
@dayne6, Thank you!!! - Margarett_Kunde commented on March 20th 20 at 11:39
@dayne6, (async () => {} )(); broke the forEach, I run it inside
array.forEach(id{
(async () => {
 const result = await db.collection("collection").findOne({colID: 1});
 const res = await db.collection('collectiontwo').insertOne({title: result.title}); 
})();
})
client.close();
- Margarett_Kunde commented on March 20th 20 at 11:42
@maryjane.Yundt, with the usual cycle will be
(async () => {
 for (let i = 0, l = array.length; i < l; ++i) {
 const result = await db.collection("collection").findOne({colID: 1});
 const res = await db.collection('collectiontwo').insertOne({title: result.title}); 
 // ...
}
client.close();
})();

but you can write an asynchronous wrapper for forEach
const asyncForEach = async (array, func) => {
 for (let i = 0, l = array.length; i < l; ++i) {
 await func(array[i], i, array);
 } 
};

asyncForEach(array, async (id) => {
 const result = await db.collection("collection").findOne({colID: 1});
 const res = await db.collection('collectiontwo').insertOne({title: result.title}); 
 // ...
}).then(() => client.close());

you can also use Promise.all, but then the database queries will be sent at the same time
Promise.all(array.map(async (id) => {
 const result = await db.collection("collection").findOne({colID: 1});
 const res = await db.collection('collectiontwo').insertOne({title: result.title}); 
 // ...
})).then(() => client.close());
- cornell_Goyette commented on March 20th 20 at 11:45
@dayne6, Thank you!!! - Margarett_Kunde commented on March 20th 20 at 11:48
@vladimir60, Thank you! - Margarett_Kunde commented on March 20th 20 at 11:54

Find more questions by tags MongoDBNode.js