【发布时间】: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