Node.js 将对象推入两级数组
我在stackoverflow上见过类似的问题,但没有一个对我有用。Node.js 将对象推入两级数组,node.js,mongodb,mongoose,mongodb-query,Node.js,Mongodb,Mongoose,Mongodb Query,我在stackoverflow上见过类似的问题,但没有一个对我有用。 我还阅读了有关更新的mongodb文档,但我所寻找的内容不在那里。 因此,我为我的模型subschool命名了subSchoolSchema var subSchoolSchema = mongoose.Schema({ schoolid: String, username: String, password: String, departments: [{ name: Stri
我还阅读了有关更新的mongodb文档,但我所寻找的内容不在那里。
因此,我为我的模型
subschool
命名了subSchoolSchema
var subSchoolSchema = mongoose.Schema({
schoolid: String,
username: String,
password: String,
departments: [{
name: String,
hod: String,
duration: Number,
classes: [{
title: String,
students: [{
schId: String,
firstname: String,
lastname: String,
othername: String,
password: String,
email: String,
group: String,
issues: Array,
notes: String,
chatId: String,
fcm_token: String,
state: Number,
type: Number,
suspended: Boolean,
details: [{
key: String,
value: String
}],
courses: [{
title: String,
code: String,
credit: Number,
instructor: String,
instructorId: String,
score: Number
}],
img: {
type: String,
default: "svg/icon_student_sync_dp.svg"
}
}]
}]
}]
})
我想使用此代码将对象推入部门.班级.学生
var stud = { schId: id, firstname: fname, lastname: lname, othername: othername, email: email, group: grp, password: password, state: 2, type: 0, img: dp, details: details }
subschool.update({ username: request.session.code, "departments.classes._id": studentClass }, {
"$push": {
"departments.classes.students" : stud
}
}, function (er, result) {
if(er){
console.log(er)
} else {
res.end('{"status" : 0,"success":1}')
}
}
我一直在犯这个错误
{ CastError: Cast to [number] failed for value "[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password"
:"7f6f5e9d","state":2,"type":0,"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]" at path "student
s"
at MongooseError.CastError (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\error\cast.js:26:11)
at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:184:15)
at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:195:15)
at castUpdateVal (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:266:22)
at walkUpdatePath (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:143:22)
at castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:71:18)
at Query._castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2902:10)
at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2752:23)
at Query.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2547:10)
at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2448:16)
at Function.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2364:10)
at C:\Users\USER\Documents\Projects\School Sync\ikey\routes\students\studentapi.js:33:15
at Query.<anonymous> (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:3800:16)
at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:277:21
at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:131:16
at _combinedTickCallback (internal/process/next_tick.js:73:7)
message: 'Cast to [number] failed for value "[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":
"7f6f5e9d","state":2,"type":0,"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]" at path "students
"',
name: 'CastError',
stringValue: '"[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":"7f6f5e9d","state":2,"type":0,
"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]"',
kind: '[number]',
value: '[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":"7f6f5e9d","state":2,"type":0,"img":"
svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]',
path: 'students',
reason:
{ CastError: Cast to number failed for value "{ schId: 'nj122',
firstname: 'djnk',
lastname: 'jjkn',
othername: 'kn',
email: 'inei@iiu.edn',
group: 'B',
password: '7f6f5e9d',
state: 2,
type: 0,
img: 'svg/icon_student_sync_dp.svg',
details:
[ { key: 'Gender', value: 'Male' },
{ key: 'Blood Type', value: 'V0' } ] }" at path "students"
at MongooseError.CastError (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\error\cast.js:26:11)
at SchemaNumber.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\number.js:231:9)
at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:180:34)
at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:195:15)
at castUpdateVal (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:266:22)
at walkUpdatePath (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:143:22)
at castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:71:18)
at Query._castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2902:10)
at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2752:23)
at Query.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2547:10)
at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2448:16)
at Function.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2364:10)
at C:\Users\USER\Documents\Projects\School Sync\ikey\routes\students\studentapi.js:33:15
at Query.<anonymous> (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:3800:16)
at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:277:21
at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:131:16
message: 'Cast to number failed for value "{ schId: \'nj122\',\n firstname: \'djnk\',\n lastname: \'jjkn\',\n othername: \'kn\',\n email: \'inei@iiu.edn\',\
n group: \'B\',\n password: \'7f6f5e9d\',\n state: 2,\n type: 0,\n img: \'svg/icon_student_sync_dp.svg\',\n details: \n [ { key: \'Gender\', value: \'Male\'
},\n { key: \'Blood Type\', value: \'V0\' } ] }" at path "students"',
name: 'CastError',
stringValue: '"{ schId: \'nj122\',\n firstname: \'djnk\',\n lastname: \'jjkn\',\n othername: \'kn\',\n email: \'inei@iiu.edn\',\n group: \'B\',\n password
: \'7f6f5e9d\',\n state: 2,\n type: 0,\n img: \'svg/icon_student_sync_dp.svg\',\n details: \n [ { key: \'Gender\', value: \'Male\' },\n { key: \'Blood Type
\', value: \'V0\' } ] }"',
kind: 'number',
value:
{ schId: 'nj122',
firstname: 'djnk',
lastname: 'jjkn',
othername: 'kn',
email: 'inei@iiu.edn',
group: 'B',
password: '7f6f5e9d',
state: 2,
type: 0,
img: 'svg/icon_student_sync_dp.svg',
details: [Object] },
path: 'students',
reason: undefined } }
{ username: 'AA',
'departments.classes._id': '59669687abf46f170045cc6e' }
{ CastError: Cast to [number] failed for value "[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password"
:"7f6f5e9d","state":2,"type":0,"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]" at path "student
s"
at MongooseError.CastError (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\error\cast.js:26:11)
at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:184:15)
at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:195:15)
at castUpdateVal (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:266:22)
at walkUpdatePath (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:143:22)
at castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:71:18)
at Query._castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2902:10)
at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2752:23)
at Query.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2547:10)
at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2448:16)
at Function.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2364:10)
at C:\Users\USER\Documents\Projects\School Sync\ikey\routes\students\studentapi.js:33:15
at Query.<anonymous> (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:3800:16)
at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:277:21
at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:131:16
at _combinedTickCallback (internal/process/next_tick.js:73:7)
message: 'Cast to [number] failed for value "[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":
"7f6f5e9d","state":2,"type":0,"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]" at path "students
"',
name: 'CastError',
stringValue: '"[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":"7f6f5e9d","state":2,"type":0,
"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]"',
kind: '[number]',
value: '[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":"7f6f5e9d","state":2,"type":0,"img":"
svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]',
path: 'students',
reason:
{ CastError: Cast to number failed for value "{ schId: 'nj122',
firstname: 'djnk',
lastname: 'jjkn',
othername: 'kn',
email: 'inei@iiu.edn',
group: 'B',
password: '7f6f5e9d',
state: 2,
type: 0,
img: 'svg/icon_student_sync_dp.svg',
details:
[ { key: 'Gender', value: 'Male' },
{ key: 'Blood Type', value: 'V0' } ] }" at path "students"
at MongooseError.CastError (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\error\cast.js:26:11)
at SchemaNumber.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\number.js:231:9)
at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:180:34)
at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:195:15)
at castUpdateVal (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:266:22)
at walkUpdatePath (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:143:22)
at castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:71:18)
at Query._castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2902:10)
at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2752:23)
at Query.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2547:10)
at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2448:16)
at Function.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2364:10)
at C:\Users\USER\Documents\Projects\School Sync\ikey\routes\students\studentapi.js:33:15
at Query.<anonymous> (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:3800:16)
at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:277:21
at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:131:16
message: 'Cast to number failed for value "{ schId: \'nj122\',\n firstname: \'djnk\',\n lastname: \'jjkn\',\n othername: \'kn\',\n email: \'inei@iiu.edn\',\
n group: \'B\',\n password: \'7f6f5e9d\',\n state: 2,\n type: 0,\n img: \'svg/icon_student_sync_dp.svg\',\n details: \n [ { key: \'Gender\', value: \'Male\'
},\n { key: \'Blood Type\', value: \'V0\' } ] }" at path "students"',
name: 'CastError',
stringValue: '"{ schId: \'nj122\',\n firstname: \'djnk\',\n lastname: \'jjkn\',\n othername: \'kn\',\n email: \'inei@iiu.edn\',\n group: \'B\',\n password
: \'7f6f5e9d\',\n state: 2,\n type: 0,\n img: \'svg/icon_student_sync_dp.svg\',\n details: \n [ { key: \'Gender\', value: \'Male\' },\n { key: \'Blood Type
\', value: \'V0\' } ] }"',
kind: 'number',
value:
{ schId: 'nj122',
firstname: 'djnk',
lastname: 'jjkn',
othername: 'kn',
email: 'inei@iiu.edn',
group: 'B',
password: '7f6f5e9d',
state: 2,
type: 0,
img: 'svg/icon_student_sync_dp.svg',
details: [Object] },
path: 'students',
reason: undefined } }
{CastError:Cast to[number]值“[{”schId:“nj122”,“firstname:“djnk”,“lastname:“jjkn”,“othername:“kn”,“email:”的转换失败inei@iiu.edn,“组”:“B”,“密码”
“7f6f5e9d”,“状态”:2,“类型”:0,“img”:“svg/icon_student_sync_dp.svg”,“详细信息”:[{”键“:”性别“,”值“:”男性“},{”键“:”血型“,”值“:”V0“}],”路径“学生”
s“
在MongooseError.CastError(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\error\cast.js:26:11)
在SchemaArray.cast(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\schema\array.js:184:15)
在SchemaArray.cast(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\schema\array.js:195:15)
在castUpdateVal(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\services\query\castUpdate.js:266:22)
在walkUpdatePath(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\services\query\castUpdate.js:143:22)
在castUpdate(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\services\query\castUpdate.js:71:18)
在查询时更新(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\Query.js:2902:10)
在更新时(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\query.js:2752:23)
在Query.update(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\Query.js:2547:10)
更新时(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\model.js:2448:16)
在Function.update(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\model.js:2364:10)
在C:\Users\USER\Documents\Projects\School Sync\ikey\routes\studentapi.js:33:15
查询时。(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\model.js:3800:16)
在C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\kareem\index.js:277:21
在C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\kareem\index.js:131:16
at _combinedTickCallback(内部/流程/下一步_tick.js:73:7)
消息:“[{”schId:“nj122”,“firstname:“djnk”,“lastname:“jjkn”,“othername:“kn”,“email:”值转换为[number]失败”inei@iiu.edn,“组”:“B”,“密码”:
“7f6f5e9d”,“state”:2,“type”:0,“img”:“svg/icon\u student\u sync\u dp.svg”,“details”:[{“key”:“Gender”,“value”:“Male”},{“key”:“Blood type”,“value”:“V0”}]}]“at path”students
"',
名称:“卡斯特罗”,
stringValue:“[{”schId:“nj122”,“firstname:“djnk”,“lastname:“jjkn”,“othername:“kn”,“email:”inei@iiu.edn,“组”:“B”,“密码”:“7f6f5e9d”,“状态”:2,“类型”:0,
“img”:“svg/icon_student_sync_dp.svg”,“details”:[{“key”:“Gender”,“value”:“Male”},{“key”:“Blood Type”,“value”:“V0”}]}],
种类:“[编号]”,
值:'[{“schId”:“nj122”,“firstname”:“djnk”,“lastname”:“jjkn”,“othername”:“kn”,“email”:”inei@iiu.edn,“组”:“B”,“密码”:“7f6f5e9d”,“状态”:2,“类型”:0,“img”:
svg/icon_student_sync_dp.svg,“细节”:[{“key”:“Gender”,“value”:“Male”},{“key”:“Blood Type”,“value”:“V0”}]}],
路径:“学生”,
原因:
{CastError:值“{schId:'nj122'”的数字转换失败,
名字:“djnk”,
姓氏:“jjkn”,
其他名称:“kn”,
电邮:'inei@iiu.edn',
B组:,
密码:“7f6f5e9d”,
国家:2,
类型:0,
img:'svg/icon\u student\u sync\u dp.svg',
细节:
[{键:'性别',值:'男性'},
路径“学生”处的“{key:'血型',值:'V0'}]}”
在MongooseError.CastError(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\error\cast.js:26:11)
在SchemaNumber.cast(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\schema\number.js:231:9)
在SchemaArray.cast(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\schema\array.js:180:34)
在SchemaArray.cast(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\schema\array.js:195:15)
在castUpdateVal(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\services\query\castUpdate.js:266:22)
在walkUpdatePath(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\services\query\castUpdate.js:143:22)
在castUpdate(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\services\query\castUpdate.js:71:18)
在查询时更新(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\Query.js:2902:10)
在更新时(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\query.js:2752:23)
在Query.update(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\Query.js:2547:10)
更新时(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\model.js:2448:16)
在Function.update(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\model.js:2364:10)
在C:\Users\USER\Documents\Projects\School Sync\ikey\routes\studentapi.js:33:15
查询时。(C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\mongoose\lib\model.js:3800:16)
在C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\kareem\index.js:277:21
在C:\Users\USER\Documents\Projects\School Sync\ikey\node\u modules\kareem\index.js:131:16
消息:“{schId:\'nj122\',\n firstname:\'djnk\',\n lastname: