简单模式不会更新

时间:2017-05-31 06:09:26

标签: mongodb meteor simple-schema

我尝试使用$ set更新MongoDB集合。集合中的字段不存在。当我尝试添加字段时,集合会暂时存储数据,然后数据消失并返回错误ClientError: name.first is not allowed by the schema。我不知道我在这里做错了什么,而且我已经在谷歌搜索了几个小时。

我正在使用:

  • 流星
  • Simple-Schema(NPM)
  • 流星collection2芯

路径: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 },
});

路径: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)
    }
});
}
});

更新

路径:call function

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

1 个答案:

答案 0 :(得分:1)

你可以尝试更换:

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

在Method.js中:

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