我试图在流星中的集合中添加来自JSON响应的数据。不幸的是,我在堆栈溢出中发现的所有东西都不起作用。在某些情况下,我返回了一个错误,如:
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。调用没有错误,它将执行以下操作:
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}});
}
}
我的Simple模式也包含以下内容:
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}});