Javascript 正在从数组中删除项不工作
我正在尝试从数组中删除标记。但我无法让它工作——什么也没发生。console.log输出似乎是正确的。谢谢 事件处理程序Javascript 正在从数组中删除项不工作,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我正在尝试从数组中删除标记。但我无法让它工作——什么也没发生。console.log输出似乎是正确的。谢谢 事件处理程序 Template.doc.events({ 'click .remove': function(event, template) { console.log("This doc's _id: " + template.data._id); console.log("doc's tags: " + template.data.metada
Template.doc.events({
'click .remove': function(event, template) {
console.log("This doc's _id: " + template.data._id);
console.log("doc's tags: " + template.data.metadata.tags);
var thisTag = this;
console.log("thisTag: " + thisTag);
MyPix.update (
template.data._id,
{ $pull: { 'template.data.metadata.tags': thisTag }},
{ multi: true }
);
},
});
模板
<template name="doc">
<ol><li>
<span class="property">metadata.tags</span>:
{{#each this.metadata.tags}}
<span class="tag"><span class="remove">X</ span>{{this}}</span>
{{/each}}
</li></ol>
</template>
metadata.tags:
{{{#每个this.metadata.tags}
X{{this}}
{{/每个}}
乍一看,pull中的字段标识符似乎不正确。这里您必须使用MongoDB标识符,而不是属于您的对象的标识符。请尝试更改
template.data.metadata.tags
到
在拉式通话中。您可以尝试类似的方式,也许
MyPix.update (
template.data._id,
{ $pull: { metadata:{ tags: thisTag } } },
{ multi: true }
);
试试接线员
Template.doc.events({
'click .remove': function(event, template) {
console.log("This doc's _id: " + template.data._id);
console.log("doc's tags: " + template.data.metadata.tags);
var thisTag = this;
console.log("thisTag: " + thisTag);
MyPix.update (
template.data._id,
{ $push: { 'template.data.metadata.tags': thisTag }},
{ multi: true }
);
},
});
如果你console.log(template.data.metadata.tags)
你得到了什么数组?
console.log(template.data.metadata.tags.length)
返回什么
您可能想查看一下结果是,
这个
是一个对象,必须转换为字符串。MongoDB标识符必须与$pull
一起使用。谢谢你的指点@埃萨恩和梅洛洛维奇
'click .remove': function(event, template) {
console.log("This doc's _id: " + template.data._id);
console.log("doc's tags: " + template.data.metadata.tags);
var thisTag = String(this); // conversion to a string
console.log("removing thisTag: " + thisTag);
MyPix.update (
template.data._id,
{ $pull: { 'metadata.tags': thisTag }}, // MongoDB identifier
);
}
谢谢@Mellowlicious,但这也不行:
错误:documentMatches需要一个文档
。我一直在尝试各种组合,但似乎没有任何效果。至少我在控制台中没有发现上面原始代码的错误–但它不会删除任何内容。如果可能,请放置一个示例文档使用push操作符,我会在数组中添加一个新的对象
(这是一个数组)@ethaand当您console.log template.data时,您是否获得了阵列?我在看其他的评论,好像你没有数组那么标签是其他数组吗?是否尝试使用$in?通过$push添加此
时,将向数组中添加一个新的[object object]
。因此,这似乎不是一个字符串,而是一个对象。也许这就是原因?因为以后可以通过$pull
成功删除这些对象。可能是这个
需要额外的手柄吗?此外,我不确定如何在此上下文中使用$。是的,请尝试使用this.tags或其他东西抱歉,但这也不起作用:(更新失败:MongoError:无法将$pull/$pullAll修饰符应用于非数组
因此我假设您的标记未存储在数组中?这是控制台中FS.File文档的复制/粘贴:标记:数组[6]0:“精灵”1:“ecity”2:“鳄鱼”3:“动物”4:“车辆”5:“测试”长度:6
(未添加或删除空格).Tags似乎是一个数组。{multi:true}
在只更新一个文档时没有意义。
'click .remove': function(event, template) {
console.log("This doc's _id: " + template.data._id);
console.log("doc's tags: " + template.data.metadata.tags);
var thisTag = String(this); // conversion to a string
console.log("removing thisTag: " + thisTag);
MyPix.update (
template.data._id,
{ $pull: { 'metadata.tags': thisTag }}, // MongoDB identifier
);
}