0
votes

I am using Simple Schema,collection hooks and Autoform packages in Meteor and I am trying to update a Embedded object in my schema in the after update collection hook. I feel I might doing something silly, but was just unable to solve this problem. I am getting the exeption while saving: Exception while invoking method '/providers/update' Error: 0 must be an integer My schema:

Schemas.Providers = new SimpleSchema({
email: {
type: String,
label: 'Email Address',
autoValue: function() {
  if (this.isInsert || this.isUpdate) {
    return Meteor.user().emails[0].address;
  }
},
autoform: {
  afFieldInput: {
    type: 'email'
  }
 }
},
officelocation: {
 type: String,
 label: 'Location of Office',
 optional:true
},
location: {
 type: [LocationSchema],
 optional:true
}});

The collection hook that works:

Providers.after.update(function (userId, doc) {
var oldDoc = this.previous;
Providers.direct.update({_id: doc._id},{$set: {location:{
    type:'Point',
    coordinates:[0,0]
}}});
});

The collection hook that does not work.. Ideally I should not be updating after the collection update, but wanted to make sure this works:

Providers.after.update(function (userId, doc) {
var oldDoc = this.previous;
var array=[];
var iArray=doc.officelocation.split(",");
array.push(Number(iArray[1]));
array.push(Number(iArray[0]))
Providers.direct.update({_id: doc._id},{$set: {location:[{
    type:'Point',
    coordinates:array
}]}}); 
});
1

1 Answers

0
votes

Looking at what you are trying to store, use parseInt instead of Number that you are using. It will return an integer that mongo can store.


The issue is not with the method that you are using. It is with the way you are storing data in mongo. Show us how your LocationSchema looks like.

Detail:

Mongo uses a different number format that what javascript uses. In javascript, a Number can be an integer, a float, a decimal or anything that you want. Mongo has very strict demands when it comes to integer or floats.

Overall, what it means is that if you want to store an accurate decimal number in mongo (which I suspect what you are trying to do), you have to either store it as a string (you loose the ability to do direct mongo operation such as $inc $gt etc) or divide it into two parts and store separably. The third option is to store it as a float which isn't accurate an is only useful if you want some kind of approximate value.