meteormeteor-collections

Meteor mongo DB is inserting sub document


Meteor collection insert is inserting subdocument instead of plain document. see my insert statement below:

return Profile.insert({
        _id: this.userId,
        "firstName": firstname,
        "lastName": lastname,
        "state": state,
        "mobile": mobile,
        "alias": alias,
        "email": email,
        "bvn": bvn,
        "createdAt": new Date(),
        "updatedAt": new Date(),
    });

And this is the result in MongoDB console:

 meteor:PRIMARY> db.profile.find().pretty()
{
    "_id" : "uNECMJJkCtQXhSs33",
    **"firstName" : {
        "firstName" : "firstName",
        "lastName" : "lastName",
        "state" : "state",
        "mobile" : 55325235522535,
        "alias" : "alias",
        "email" : "email",
        "bvn" : 6364634664
    },**
    "createdAt" : ISODate("2018-12-15T03:23:33.243Z"),
    "updatedAt" : ISODate("2018-12-15T03:23:33.243Z")
}

Below is my expectation

    meteor:PRIMARY> db.profile.find().pretty()
{
    "_id" : "uNECMJJkCtQXhSs33",
    "firstName" : "firstName",
    "lastName" : "lastName",
    "state" : "state",
    "mobile" : 55325235522535,
    "alias" : "alias",
    "email" : "email",
    "bvn" : 6364634664
    "createdAt" : ISODate("2018-12-15T03:23:33.243Z"),
    "updatedAt" : ISODate("2018-12-15T03:23:33.243Z")
}

Solution

  • I have resolved the issues. I had to pass all the parameters as object from the template into the method as follow:

    Template

     const updateProfile = {
                "alias":target.aliasName.value, 
                "email":target.email.value, 
                "state":target.state.value, 
                "mobile":target.phone.value
        }
    
        Meteor.call('profileUpdate', updateProfile);
    

    Method :

    Meteor.methods({
    'profileInsert'(profile) {
        if (!this.userId) {
            throw new Meteor.Error('not-authorized');
        }
    
        Match.test(profile.firstname, String);
        Match.test(profile.lastname, String);
        Match.test(profile.state, String);
        Match.test(profile.mobile, Number);
        Match.test(profile.bvn, Number);
        Match.test(profile.email, String);
        Match.test(profile.alias, String);
    
        return Profile.insert(profile);
    }  });