【问题标题】:Simple-schema wont update简单模式不会更新
【发布时间】:2017-05-31 15:56:22
【问题描述】:

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

我正在使用:

  • 流星
  • 简单模式 (NPM)
  • meteor-collection2-core

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

【问题讨论】:

    标签: mongodb meteor simple-schema


    【解决方案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 }),
    

    【讨论】:

    • 那行不通。错误Exception while invoking method 'profileCandidate.updateContactDetails' ClientError: name.first is not allowed by the schema. at Array.forEach。我在上面添加了 ProfileCandidate.call。
    • 这就是你想要的吗?
    • 这似乎行得通。我真的不了解验证器功能。
    • 如果你想定义一个对象,你必须使用一个子模式。这记录在here。如果它对您有用,请将答案标记为正确。文档有点长,但值得一读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    相关资源
    最近更新 更多