Javascript 无法从graphql数据库更新mongodb集合
我无法解决以下问题。我开始使用NodeJS、Apollo和Mongoose开发GraphQL服务器 以下是数据库中的一些示例数据: 处方设置Javascript 无法从graphql数据库更新mongodb集合,javascript,node.js,mongodb,mongoose,graphql,Javascript,Node.js,Mongodb,Mongoose,Graphql,我无法解决以下问题。我开始使用NodeJS、Apollo和Mongoose开发GraphQL服务器 以下是数据库中的一些示例数据: 处方设置 { "_id": "5ea99ae6fc48d036e083ec20", "name": "Calcium", "valueTo": -51, "valueFrom": -75, "treatmen
{
"_id": "5ea99ae6fc48d036e083ec20",
"name": "Calcium",
"valueTo": -51,
"valueFrom": -75,
"treatmentDescription": "Deficiency",
"recommendation": "<p><strong>Calcium Deficiency</strong></p>\n<p><strong>S & S include: </strong></p>\n<p>Anxiety, Muscle cramps, and spasms, Bruising, Nervousness, Insomnia, Osteoporosis, Tooth decay</p>\n<p><strong>Calcium sources:</strong></p>\n<p>Milk, cheese and other dairy foods. Broccoli, cabbage and okra, Soya beans, Nuts, Flour, Fish</p>",
"isNormal": false
},
{
"_id": "5ea99ae6fc48d036e083ec21",
"name": "Calcium",
"valueTo": 100,
"valueFrom": 76,
"treatmentDescription": "High Bio-unavailable",
"recommendation": "<p><strong>Calcium Excess</strong></p>\n<p><strong>S & S include:</strong></p>\n<p>Arthritis, Gall stones, Constipation, Kidney stones, Depression, Mental, Fatigue.</p>\n<p><strong>Calcium sources:</strong></p>\n<p>Milk, cheese and other dairy foods.Broccoli, cabbage and okra, Soya beans, Nuts, Flour, Fish</p>",
"isNormal": false
},
{
"_id": "5ea99ae6fc48d036e083ec89",
"name": "Calcium",
"valueTo": -26,
"valueFrom": -50,
"treatmentDescription": "Border line deficiency",
"recommendation": "<p><strong>Calcium Borderline Deficiency</strong></p>\n<p><strong>S & S include: </strong></p>\n<p>Fatigue.Weak and brittle fingernails.Poor appetite. Muscle cramps, stiffness, and spasms.</p>\n<p><strong>Calcium sources:</strong></p>\n<p>Milk, cheese and other dairy foods. Broccoli, cabbage and okra, Soya beans.<br /> Nuts. Flour. Fish</p>",
"isNormal": false
},
{
"_id": "5ea99ae6fc48d036e083ec8a",
"name": "Calcium",
"valueTo": -76,
"valueFrom": -100,
"treatmentDescription": "insufficiency",
"recommendation": "<p><strong>Calcium Insufficiency</strong> <br /><strong>S & S include: </strong></p>\n<p>Anxiety, Muscle cramps and spasms, Bruising, Nervousness, Insomnia, Osteoporosis, Tooth decay</p>\n<p><strong>Calcium sources:</strong></p>\n<p>Milk, cheese and other dairy foods.Broccoli, cabbage and okra, Soya beans , Nuts , Flour , Fish</p>",
"isNormal": false
},
{
"_id": "5ea99ae6fc48d036e083ec22",
"name": "Magnesium",
"valueTo": 100,
"valueFrom": 76,
"treatmentDescription": "High Bio-unavailable",
"recommendation": "<p><strong>Magnesium Excess</strong></p>\n<p><strong>S & S include:</strong></p>\n<p>Confusion, Fatigue, Depression, Low blood pressure, Diarrhea, Muscle weakness</p>\n<p><strong>Magnesium sources:</strong></p>\n<p>Spinach, figs, avocado, banana and raspberries. Nuts and seeds. Legumes. Peas, broccoli, cabbage, green beans, artichokes, Seafood</p>",
"isNormal": false
},
{
"_id": "5ea99ae6fc48d036e083ec53",
"name": "Magnesium",
"valueTo": 25,
"valueFrom": -25,
"treatmentDescription": "Normal / Ideal zone",
"recommendation": "",
"isNormal": true
},
我想做的是,从解析的DPDF上的每个数组中获取“名称”和“值”,检查“值””位于处方设置中的“值from”和“值to”之间的位置,并更新“详细信息”以及“说明”和“建议”
这就是我所做的。我创造了一个变异:
updateParsePdf: authenticated(async (parent, args, context, info) => {
try {
const pdf = await ParsePdf.findById(args._id);
const newReport = await updatedReport(pdf._doc);
const reportUpdated = await ParsePdf.findByIdAndUpdate(args._id, {
newReport,
}).exec();
return reportUpdated._doc;
} catch (error) {
console.log("error: ", error);
throw new AuthenticationError("Opps! Something went wrong.", error);
}
}),
更新报告方法
const updatedReport = async (pdf) => {
//pdf is the response from the ParsedPdf above
let updated = {};
try {
await Object.keys(pdf).forEach((key) => {
const field = pdf[key];
if (Array.isArray(field)) {
const newRep = field.map(async (f) => {
const pp = PrescriptionSetup.find({
name: f.name,
valueFrom: {
$lte: f.value,
},
valueTo: {
$gte: f.value,
},
})
.exec()
.then((p) => {
return {
...f,
details: {
description: p[0].treatmentDescription,
recommendation: p[0].recommendation,
isNormal: p[0].isNormal,
},
};
})
.catch((e) => {
console.log("Error finding setup: ", e);
});
return pp;
});
updated = { ...updated, [key]: [...newRep] };
}
});
const aa = { ...pdf, ...updated };
return aa;
} catch (error) {
console.log("Error...: ", error);
}
};
ParsedPdf集合中有很多数组,其中包含很多项。我认为由于巨大的数据库查询,我无法使它工作。
解决这个问题的最佳方法是什么
感谢您的帮助我将建议一种代码更改最少的方法
const pp=PrescriptionSetup。在这里查找pp
是一个Promise
,因此newRep
将是Promise
的数组
而不是[…newRep]
使用[…等待承诺。all(newRep)]
wait Object.keys(pdf).forEach((key)=>…)
此处。forEach
不返回任何内容,实际上您不必wait
,但我们在(1.)中添加了一个异步逻辑,所以我们必须处理它
更改为wait Promise.map(Object.keys(pdf),async(key)=>…)
如果您使用bluebird
,则使用与Promise.map等效的东西你确定
ParsedPdf
中的value
是一个String
?而valueFrom
,valueTo
是Number
s吗?有什么不起作用的是出现了错误或者它没有按照它应该的那样更新?我观察到的问题主要与异步操作有关。你忘了把wait
在一些应该出现的地方,你把它们放在不应该出现的地方appear@TheeSritabtim你能告诉我等待的正确位置吗?查找
查询正在运行。谢谢。这解决了问题。以前没有听说过蓝鸟
,尝试一下。谢谢你的建议
const updatedReport = async (pdf) => {
//pdf is the response from the ParsedPdf above
let updated = {};
try {
await Object.keys(pdf).forEach((key) => {
const field = pdf[key];
if (Array.isArray(field)) {
const newRep = field.map(async (f) => {
const pp = PrescriptionSetup.find({
name: f.name,
valueFrom: {
$lte: f.value,
},
valueTo: {
$gte: f.value,
},
})
.exec()
.then((p) => {
return {
...f,
details: {
description: p[0].treatmentDescription,
recommendation: p[0].recommendation,
isNormal: p[0].isNormal,
},
};
})
.catch((e) => {
console.log("Error finding setup: ", e);
});
return pp;
});
updated = { ...updated, [key]: [...newRep] };
}
});
const aa = { ...pdf, ...updated };
return aa;
} catch (error) {
console.log("Error...: ", error);
}
};