Search code examples
mongodbmeteorsimple-schema

Simple-schema wont update


I'm trying to update a MongoDB collection using $set. The field within the collection doesn't exist. When I try to add the field, the collection briefly stores the data and then the data disappears and returns an error ClientError: name.first is not allowed by the schema. I have no idea what I'm doing wrong here and I've been searching google for hours.

I'm using:

  • Meteor
  • Simple-Schema (NPM)
  • meteor-collection2-core

Path: Simple-Schema

const ProfileCandidateSchema = new SimpleSchema({
  userId: {
    type: String,
    regEx: SimpleSchema.RegEx.Id,
  },
  createdAt: {
    type: Date,
  },
  name: { type: Object, optional: true },
    'name.first': { type: String },
});

Path: Method.js

import { Meteor } from 'meteor/meteor';
import { ProfileCandidate } from '../profileCandidate';
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';

export const updateContactDetails = new ValidatedMethod({
  name: 'profileCandidate.updateContactDetails',

validate: new SimpleSchema({
    'name.first': { type: String },
  }).validator({ clean: true }),

run(data) {
  ProfileCandidate.update({userId: this.userId},
    {$set:
      {
        name: {'first': "Frank"},
      }
    }, (error, result) => {

    if(error) {
      console.log("error: ", error)
    }
});
}
});

UPDATE

Path: call function

updateContactDetails.call(data, (error) => {
  if (error) {
    console.log("err: ", error);
          console.log("err.error: ", error.error);
  }
});

Solution

  • Can you try replacing:

    validate: new SimpleSchema({
      'name.first': { type: String },
    }).validator({ clean: true }),
    

    in Method.js by:

    validate: new SimpleSchema({
      name: {
        type: new SimpleSchema({
          first: String,
        }),
        optional: true,
    }).validator({ clean: true }),