2017-07-07 113 views
0

我想插入一个数组到对象中,我没有任何运气。我认为架构基于验证拒绝它,但我不知道为什么。如果我console.log(this.state.typeOfWork)和检查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] 
     } 
     } 
    }); 
+0

你能展示你的整个更新调用和集合模式吗? – mparkitny

回答

0

typeOfWorkArray。你应该在它把你的价值:

$push: { 
    "roleAndSkills.typeOfWork": this.state.typeOfWork 
} 

多个值:

$push: { 
    "roleAndSkills.typeOfWork": { $each: [ "val1", "val2" ] } 
} 

mongo $push operator

mongo dot notation

+0

我得到这个错误:'未捕获的错误:筛选出不在模式中的键后,您的修饰符现在为空' – bp123

+0

双引号不会返回错误,但是它也不会添加到数组中。没有错误。 – bp123

+0

您是否尝试推送一个或多个值? –

0

简单的模式有一些问题与对象或数组的验证,我有我最近开发的一个应用程序也出现同样的问题

你能做什么? 好,我做什么,在Collections.js文件,当你说:

typeOfWork:{ 
    type: Array 
} 

尝试添加属性黑盒:真正的,就像这样:

typeOfWork:{ 
    blackbox: true, 
    type: Array 
} 

这会告诉你的模式,它此字段正在使用数组,但忽略进一步验证。

我做的验证是在main.js上,只是为了确保我没有空数组而且数据是纯文本。

按照这里要求的是我的更新方法,即时我的情况我使用的对象不是数组,但它的工作方式相同。

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); 

希望它可以帮助

罗布

+0

尝试这个,但没有更多的运气。你是如何编写更新方法的? – bp123

+0

你没有检查我的编辑@ bp123 – RSamurai

+0

没有。 Simpleschema让我疯狂。 – bp123

0

幽州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] } 

请注意,对象和数组默认是可选的,因此您甚至可以省略可选标志。

+0

我很久以前就试过了。出于某种原因,它不起作用。阵列真的让我感到困惑。 – bp123