我试图在我的流星集合中添加来自 JSON 响应的数据。不幸的是,我在 Stack Overflow 上发现的所有东西都不起作用。在某些情况下,我收到一个错误,例如:
Exception in callback of async function: MongoError: Cannot update 'plugins' and 'plugins' at the same time
如果它没有返回错误,我的集合中的对象保持为空,例如:
"plugins": {}
下面的代码导致了一个空对象,我希望这里有人可以帮助我。
我的集合由一个字段组成,该字段具有一个对象,其中包含多个对象。我希望他们像这样
{
"_id" : "id",
"name" : "DemoSite",
"url" : "http://someurl.com",
"createdAt" : ISODate("2016-02-10T17:22:15.011+0000"),
"plugins" :
"Akismet": {
"pluginName" : "Akismet",
"currentPluginVersion" : "3.1.7",
"newPluginVersion" : null,
"lastChecked" : "Thu Feb 18 2016 15:54:02 GMT+0100 (CET)"
},
"Random Plugin": {
"pluginName" : "Random Plugin",
"currentPluginVersion" : "0.1.0",
"newPluginVersion" : null,
"lastChecked" : "Thu Feb 18 2016 15:54:02 GMT+0100 (CET)"
}
}
如果我的 HTTP.call 没有错误,它将执行以下操作:
var pluginData = result.data.plugins;
var dbPush = {};
if(pluginData != []){
for (var key in pluginData){
var obj = pluginData[key];
var objKey = obj.Name;
dbPush[objKey] = {
'pluginName': objKey,
'currentPluginVersion': obj.Version,
'newPluginVersion': null,
'lastChecked': new Date()
};
Items.update({_id: item._id}, {$set: {plugins: dbPush}});
}
}
我的简单模式也有这个:
Items.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Item name",
min: 4,
max: 100
},
url: {
type: String,
label: "Item url",
autoform: {
afFieldInput: {
type: "url"
}
}
},
createdAt: {
type: Date,
optional: true,
autoValue: function() {
if (this.isInsert) {
return new Date();
} else {
this.unset(); // Prevent user from supplying their own value
}
},
autoform: {
afFieldInput: {
type: "hidden"
}
}
},
plugins: {
type: Object,
optional: true
}
}));
编辑
我试图预定义所有对象,然后更新集合
dbPush["test"] = {
'pluginName': "test",
'currentPluginVersion': "1.0.0",
'newPluginVersion': null,
'lastChecked': new Date()
};
Items.update({_id: item._id}, {$set: {plugins: dbPush}});