[英]How to use Javascript variable in the value portion of a JSON?
var avg_temp = req.params.rating;
var destination = req.params.name;
var temp = { "results.name" : destination };
var wew = {$set: {"results.$.rating" :avg_temp} };
mach.update(temp ,wew , function(err,jop){
if(err)
{
res.status(500);
res.send('Destination not found');
}
else
{
res.status(202);
res.send('Rating is posted');
}
});
Sample document :示例文件:
{
"_id" : ObjectId("56247581846ec164bb6be7bf"),
"results" : [
`enter code here`{
"name" : "Mk.Mechanics",
"ph" : NumberLong(9899991817),
"error" : false,
"lat" : 28.5833958899999980,
"lon" : 77.1687398899999980,
"rating" : 3,
"add" : "1123, Nanak pura, Delhi, 110021, India",
"local" : "Nanak pura",
"city" : "Delhi",
"contry" : "India"
},
{
"name" : "Pahadi",
"ph" : NumberLong(9876591817),
"error" : false,
"lat" : 28.6776553099999990,
"lon" : 77.2616931799999950,
"rating" : 3.5000000000000000,
"add" : "plot-6 saranshpur, Shastri park,Delhi,110018, India",
"local" : "saranshpur",
"city" : "Delhi",
"contry" : "India"
},
{
"name" : "mirza",
"ph" : 1.23123e+007,
"error" : false,
"lat" : 28.6775564800000000,
"lon" : 77.2621437899999960,
"rating" : 1,
"add" : " plopur, Shastri park, Delhi,110099,India",
"local" : "Shastri park",
"city" : "Delhi",
"contry" : "India"
}
] }
When i use static value in destination and avg_temp (eg. "Pahadi" and 12 respectively) this works but in this case it does not当我在目标和 avg_temp 中使用静态值(例如分别为“Pahadi”和 12)时,这有效,但在这种情况下它不起作用
Request is a patch request which I am making to the Node server :请求是我向节点服务器发出的补丁请求:
localhost:8000\api\machs?name=Pahadi&rating=1
Any idea How to access JavaScript object in json任何想法如何访问 json 中的 JavaScript 对象
Output : Destination Not Found (Eventhough i've put exact string values in name params)输出:未找到目的地(即使我在名称参数中输入了精确的字符串值)
just put a console.log(err);
只需放一个console.log(err);
above res.status(500);
以上res.status(500);
. . So that we can know what the error is from DB.这样我们就可以知道错误来自 DB。
Ahh Silly Mistake , Changed req.params to req.query as the values were sent by the http request in query string !啊,愚蠢的错误,将 req.params 更改为 req.query,因为值是由查询字符串中的 http 请求发送的! Problem solved , Now the code works as it should .问题解决了,现在代码可以正常工作了。 Thank you guys for participating though :)不过还是谢谢大家的参与:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.