1

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);
  }
});
bp123
  • 3,217
  • 8
  • 35
  • 74

1 Answers1

1

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 }),
Damien Monni
  • 1,418
  • 3
  • 15
  • 25
  • That didn't work. Error `Exception while invoking method 'profileCandidate.updateContactDetails' ClientError: name.first is not allowed by the schema. at Array.forEach`. I add the ProfileCandidate.call above. – bp123 May 31 '17 at 06:32
  • Is that what you wanted? – bp123 May 31 '17 at 07:01
  • 1
    That seems to work. I'm really not understanding the validator function. – bp123 May 31 '17 at 07:18
  • If you want to define an object, you have to use a subschema. This is documented [here](https://github.com/aldeed/node-simple-schema#subschemas). If it works for you, please mark the answer as correct. The doc is a bit long but worth to read. – Damien Monni May 31 '17 at 07:47