Reputation: 1
I'm using MongoDB, and have a connection and a client that work. When you run the following code, you can console.log() the result and see the proper output:
connect.then(async () => {
const dbo = client.db(prod == 1 ? "prodData" : "test");
let data = await dbo.collection("practitioners").findOne({"_id" : "Oq1tOesempf3jjLfrVrWaVtaROI3"}, function(err, result) {
if (err) throw err;
console.log(result); // works!!!
return result;
});
});
However, I'm trying to get the result outside of the function but I can't figure out the await/async part of this or how to do it as a promise. I've been fumbling around with this for a long time to help would be greatly appreciated:
const find = async (connect, client, prod = 0) => {
// this works
// let data2 = await axios.get('https://reqres.in/api/users?page=2');
// console.log(data2);
let data = await connect.then(async () => {
const dbo = client.db(prod == 1 ? "prodData" : "test");
await dbo.collection("practitioners").findOne({"_id" : "Oq1tOesempf3jjLfrVrWaVtaROI3"}, function(err, result) {
if (err) throw err;
return result;
});
});
console.log(data); // undefined should be value :(
return data;
}
Even something like this might help if you can get the data outside of the response:
connect.then(() => {
const dbo = client.db(prod == 1 ? "prodData" : "test");
let data = await dbo.collection("practitioners").findOne({"_id" : "Oq1tOesempf3jjLfrVrWaVtaROI3"}, function(err, result) {
if (err) throw err;
return result;
});
console.log(data); // undefined should be value :(
return data;
});
If someone can help me and guide me a bit more about explain exactly which parts of these functions are async and values are await, I would be very thankful. Also if you don't mind writing it as a promise with resolve, that might help me understand a bit more.
Upvotes: 0
Views: 391
Reputation: 62676
Mongo methods mostly return promises, and (it's my understanding) continue to take callbacks to keep older apps working, but the two approaches don't mix, and promises are the way to go for new code.
When using promises, there's an old (then/catch) vs. new (async/await) syntax choice which there's no reason to mix. I think it's better to learn promises in the older style so...
function getPractitioners(id) {
const dbo = client.db(prod == 1 ? "prodData" : "test");
return dbo.collection("practitioners").findOne({"_id" : id}) // returns a promise
}
getPractitioners("Oq1tOesempf3jjLfrVrWaVtaROI3").then(result => {
console.log(result);
}).catch(err => {
console.log(err);
});
In the newer syntax (I'd still advise learning the old way first)...
async function getPractitioners(id) {
const dbo = client.db(prod == 1 ? "prodData" : "test");
return dbo.collection("practitioners").findOne({"_id" : id}) // still returns a promise, but can be await-ed
}
try {
let result = await getPractitioners("Oq1tOesempf3jjLfrVrWaVtaROI3");
console.log(result);
} catch (err) {
console.log(err)
}
Upvotes: 1
Reputation: 173
You can use the following:
const mongoose = require('mongoose')
// connectionString is the link to your database
mongoose.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true})
const db = mongoose.connection
db.on('error', () => console.log("Couldn't connect to DB"))
db.once('open', () => {
console.log('Connected to mongoDB!')
// Add your code in here
)}
Upvotes: 0