我正在尝试将一个数组插入到一个对象中,但我没有任何运气。我认为模式是基于验证来拒绝它的,但我不确定为什么。如果我检查并检查typeof,它会指出它是一个包含以下内容的Object:
(2) ["Audit - internal", "Audit - external"]
0: "Audit - internal"
1: "Audit - external"我的集合在更新后包含:
"roleAndSkills": {
"typeOfWork": []
}示例:Schema
roleAndSkills: { type: Object, optional: true },
'roleAndSkills.typeOfWork': { type: Array, optional: true },
'roleAndSkills.typeOfWork.$': { type: String, optional: true }示例:update
ProfileCandidate.update(this.state.profileCandidateCollectionId, {
$set: {
roleAndSkills: {
typeOfWork: [this.state.typeOfWork]
}
}
});发布于 2017-07-07 16:21:48
typeOfWork是一个Array。你应该把你的价值放在里面:
$push: {
"roleAndSkills.typeOfWork": this.state.typeOfWork
}对于多个值:
$push: {
"roleAndSkills.typeOfWork": { $each: [ "val1", "val2" ] }
}发布于 2017-07-07 17:17:04
Simple schema在对象或数组上的验证有一些问题,我最近开发的一个应用程序也有同样的问题。
你能做什么?好吧,我在Collections.js文件上做了什么,当你说:
typeOfWork:{
type: Array
}尝试添加属性blackbox:true,如下所示:
typeOfWork:{
blackbox: true,
type: Array
}这将告诉您的Schema该字段接受一个Array,但忽略进一步的验证。
我的验证是在main.js上进行的,只是为了确保我没有空数组,并且数据是纯文本的。
这里请求的是我的update方法,在我的例子中,我使用的是对象而不是数组,但它的工作方式相同。
editUser: function (editedUserVars, uid) {
console.log(uid);
return Utilizadores.update(
{_id: uid},
{$set:{
username: editedUserVars.username,
usernim: editedUserVars.usernim,
userrank: {short: editedUserVars.userrank.short,
long: editedUserVars.userrank.long},
userspec: {short: editedUserVars.userspec.short,
long: editedUserVars.userspec.long},
usertype: editedUserVars.usertype}},
{upsert: true})
},在这里它是集合模式
UtilizadoresSchema = new SimpleSchema({
username:{
type: String
},
usernim:{
type: String
},
userrank:{
blackbox: true,
type: Object
},
userspec:{
blackbox: true,
type: Object
},
usertype:{
type: String
}
});
Utilizadores.attachSchema(UtilizadoresSchema);希望能有所帮助
抢夺
发布于 2017-07-08 07:17:05
您声明this.state.typeOfWork是一个(字符串数组),但是当您.update()您的文档时,您将它括在方括号中:
ProfileCandidate.update(this.state.profileCandidateCollectionId, {
$set: {
roleAndSkills: {
typeOfWork: [this.state.typeOfWork]
}
}
});只需删除多余的方括号:
ProfileCandidate.update(this.state.profileCandidateCollectionId, {
$set: {
roleAndSkills: {
typeOfWork: this.state.typeOfWork
}
}
});此外,由于您的数组只是一个字符串数组,因此您可以通过将其声明为类型的[String]来稍微简化您的模式:
'roleAndSkills.typeOfWork': { type: [String] }此外,请注意,默认情况下,对象和数组是可选的,因此您甚至可以省略可选标志。
https://stackoverflow.com/questions/44961938
复制相似问题