I tried to add data from a JSON response in my collection in meteor. Unfortunately all the things I found on Stack overflow didn't work. In some cases I got an error back like:
Exception in callback of async function: MongoError: Cannot update 'plugins' and 'plugins' at the same time
and if it didn't return an error my object in my collection stayed empty like:
"plugins": {}
The code below is what resulted in an empty object, I hope someone here can help my out.
My collection consist of a field which has an object with multiple objects in it. I want them to be like this
{
"_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)"
}
}
If my HTTP.call has no error it will do this:
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}});
}
}
Also my Simple schema has this in it:
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
}
}));
I tried to predefine all the object and then update the collection
dbPush["test"] = {
'pluginName': "test",
'currentPluginVersion': "1.0.0",
'newPluginVersion': null,
'lastChecked': new Date()
};
Items.update({_id: item._id}, {$set: {plugins: dbPush}});
I found a fix.
It's not a great solution but Simple Schema doesn't allows me to have custom objects in my collection. All that's needed to get this to work with my collection is the blackbox option found in the documentation here.
plugins: {
type: Object,
optional: true,
blackbox: true
}
This will skip validation on the plugins
object so it will store the data in the collection.
Thanks for all the help!