使用$ set中的变量更新带有node.js的mongodb(update mongodb with node.js using a variable in $set)

编程入门 行业动态 更新时间:2024-10-19 08:57:48
使用$ set中的变量更新带有node.js的mongodb(update mongodb with node.js using a variable in $set)

我正在制作投票系统,投票是通过链接完成的。 在我的index.js中,我得到所需的值并将它们放入变量中。 “type”变量代表我需要更新的mongodb中的字段,我已经把它放在一个变量中,因为它取决于哪个链接被点击。

现在在$ set函数中,它们需要db字段和一个新值,这两个函数都使用变量,但我的“类型”变量不起作用。 当我去到我的MongoDB时,有一个新的表格被创建,称为“类型”。 这怎么解决?

router.get('/vote/:Id/:Value/:Type', function(req, res) { var db = req.db; var id = req.params.Id; var type = req.params.Type; var value = parseInt(req.params.Value); var newValue = value + 1; var collection = db.get('games'); collection.update( {"_id" : id}, {$set: {type: newValue}} , function (err, doc) { if (err) { res.send("There was a problem"); } else { res.location("../../../admin"); res.redirect("../../../admin"); } });

});

I am making a voting system, the voting is done with a link. In my index.js I get the required values and put them in variables. The "type" variable stands for the field in my mongodb wich needs to be updated, I have put it in a variable because it depends on which link is clicked.

Now in the $set function they require the db field and a new value, for both I use variables but my "type" variable doesn't work. And when I go to my mongodb there is a new table created called "type". How can this be solved?

router.get('/vote/:Id/:Value/:Type', function(req, res) { var db = req.db; var id = req.params.Id; var type = req.params.Type; var value = parseInt(req.params.Value); var newValue = value + 1; var collection = db.get('games'); collection.update( {"_id" : id}, {$set: {type: newValue}} , function (err, doc) { if (err) { res.send("There was a problem"); } else { res.location("../../../admin"); res.redirect("../../../admin"); } });

});

最满意答案

在javascript中,你不能使用变量作为对象文字中的属性名称,这就是你正在做的。

尝试一下:

var a = 'someProperty'; var o = {a: 'somePropertyValue'}; console.log(o);

将打印{ a: 'somePropertyValue' }不是{someProperty:'somePropertyValue} 。

如果javascript允许在对象文字表示法中引用属性名称中的变量,则它必须除去未加引号的名称,因为这会导致不明确的名称。 a应该用作属性的值还是应该是变量a的值?

尝试使用事先创建的对象crate创建更新对象文本,而不使用对象文字符号,因此您的代码如下所示:

router.get('/vote/:Id/:Value/:Type', function(req, res) { var db = req.db; var id = req.params.Id; var type = req.params.Type; var value = parseInt(req.params.Value); var newValue = value + 1; var collection = db.get('games'); //We create the $set property/value pair using property assignment, not the object literal var updateVal = {}; updateVal[type] = newValue; collection.update( {"_id" : id}, {$set: updateVal} //use it here , function (err, doc) { if (err) { res.send("There was a problem"); } else { res.location("../../../admin"); res.redirect("../../../admin"); } } ); });

更好的是,预先构建整个$set操作。

In javascript you cannot use variables as property names in object literals, and that's what you're trying to do.

Try it:

var a = 'someProperty'; var o = {a: 'somePropertyValue'}; console.log(o);

will print { a: 'somePropertyValue' } not {someProperty:'somePropertyValue}.

If javascript permitted referencing variables in property names in object literal notation it would have to get rid of unquoted names as those would create ambiguity. Should a be used as the value of the property or should it be the value of the variable a?

Try creating the update object literal with an object crated beforehand without the usage of object literal notation, so your code looks something like this:

router.get('/vote/:Id/:Value/:Type', function(req, res) { var db = req.db; var id = req.params.Id; var type = req.params.Type; var value = parseInt(req.params.Value); var newValue = value + 1; var collection = db.get('games'); //We create the $set property/value pair using property assignment, not the object literal var updateVal = {}; updateVal[type] = newValue; collection.update( {"_id" : id}, {$set: updateVal} //use it here , function (err, doc) { if (err) { res.send("There was a problem"); } else { res.location("../../../admin"); res.redirect("../../../admin"); } } ); });

Even better, construct the whole $set operation beforehand.

更多推荐

本文发布于:2023-04-29 12:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   node   set   js   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!