This is a GraphQL resolver. The problem is with promise handling using async/await.
I have tried to implement promise handling, but I am not able to set it the right way, I do not have much experience in promise handling, some study materials will do a great help.
My understanding was that the script will stop where await is called and will continue after the await call is finished. but it is bypassing the await. await call is finished after the value is returned
allDocs: async (args, context) => context().then(async client => {
let db = client.db(dbName)
const id = args.identifier.trim()
let nameArr = []
return await db.collection("collection").find({
$or: [{
"a.iden": id
}, {
"b.iden": id
}]
}).toArray().then(async (arr) => {
let year
arr.map(async element => {
let pmcid = element.pmc_id.toUpperCase()
try {
year = await db.collection("Another_collection").findOne({"pmcid" : query})["year"]
} catch (e) {
year = null
}
element["publication_year"] = year
})
return await arr
}).then((arr)=>{
client.close()
return {
"documents": arr,
"count": arr.length,
"searchkey": id
}
})
}),
The expected return value should have "publication_year" as some year, it is giving null now.
Thanks for help