gpt4 book ai didi

node.js - 如何将对象数组推送到子文档

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:52 25 4
gpt4 key购买 nike

我有一个成员模型,其中家庭成员是一个子文档,我正在尝试将对象数组推送到子文档中!请帮帮我

成员(member)模型

const memberSchema = new Schema({
fullName: {
type: String,
required: true,
trim: true
},
gender: {
type: String,
required: true
},
dateOfBirth: {
type: String,
required: true
},
age: {
type: Number,
required: true
},
caste: {
type: String,
required: true
},
education: {
type: String,
required: true
},
occupation: {
type: String,
required: true
},
profession: {
type: String,
required: true
},
address: {
street: {
type: String,
required: true
},
city: {
type: String,
required: true
},
district: {
type: String,
required: true
}
},
jobAlerts: {
type: String,
enum: ['yes', 'no'],
required: true
},
jobTraining: {
type: String,
enum: ['yes', 'no'],
required: true
},
// state: {
// type: String,
// required: true,
// trim: true
// },
// taluk:{
// type: String,
// required:true,
// trim:true
// },
userId: {
type: Schema.Types.ObjectId,
ref: "User"
},
familyMembers: [{ familyMemberSchema }],
isDeleted: {
type: Boolean,
default: false
},
verificationStatus: {
type: Boolean,
default: false
},
paymentStatus: {
type: Boolean,
default: false
},
referredBy: {
type: String,
required: true
}
});

memberSchema.plugin(timeStamp);
const Member = mongoose.model("Member", memberSchema);
<小时/>

家庭成员架构

const familyMemberSchema = new Schema({
familyReference: {
type: Schema.Types.ObjectId,
required: true,
ref: 'Member'
},
fullName: {
type: String,
required: true
},
gender: {
type: String,
required: true
},
dateOfBirth: {
type: String,
required: true
},
age: {
type: Number,
required: true
},
education: {
type: String,
required: true
},
occupation: {
type: String,
required: true
},
profession: {
type: String,
required: true
},
userId: {
type: Schema.Types.ObjectId,
required: true,
ref: 'User'
},
isDeleted: {
type: Boolean,
default: false
}
})

module.exports = {
familyMemberSchema
}
<小时/>

用户 Controller

router.post("/register/member", authenticateUser, authorizeVolunteer, async (req, res) => {
let body;
let volunteer = req.user.username
try {
body = req.body
// User.findOne(body.mobile).then(registeredUser =>{
// if(!registeredUser){

// }
// })
if (body.familyMembers.length !== 0) {
let user = new User()
let member = new Member()
user.username = body.username
user.mobile = body.mobile
user.password = body.password
let newUser = await user.save()
member.fullName = body.fullName
member.gender = body.gender
member.dateOfBirth = body.dateOfBirth
member.age = body.age
member.caste = body.caste
member.education = body.education
member.occupation = body.occupation
member.profession = body.profession
member.jobAlerts = body.jobAlerts
member.jobTraining = body.jobTraining
member.address.street = body.address.street
member.address.city = body.address.city
member.address.district = body.address.district
body.familyMembers.forEach((familyMember) => {
// return (
User.findOne({ mobile: familyMember.mobile }).then(registeredUser => {
if (!registeredUser) {
let user = new User()
user.username = familyMember.username
user.mobile = familyMember.mobile
user.password = familyMember.password
let newMember = {}
newMember.fullName = familyMember.fullName
newMember.gender = familyMember.gender
newMember.dateOfBirth = familyMember.dateOfBirth
newMember.age = familyMember.age
newMember.education = familyMember.education
newMember.occupation = familyMember.occupation
newMember.profession = familyMember.profession
newMember.userId = user._id
newMember.familyReference = member._id
member.familyMembers.push(newMember)
user.save()
// .then(newUser => console.log(newUser))
} else {
res.send("User already exists")
}
})
// )
})
// console.log(body.familyMembers)
console.log(member.familyMembers)
member.verificationStatus = true
member.referredBy = volunteer
let newMember = await member.save()
res.send({
notice: "Successfully Registered",
newMember
})

} else {
const { username, age, mobile, password, fullName, gender, dateOfBirth, caste, education, occupation, profession, jobAlerts, jobTraining, address: { street, city, district } } = body

User.findOne({ mobile } || { username }).then(registeredUser => {
if (!registeredUser) {

let user = new User({ username, mobile, password })

let member = new Member({ fullName, age, gender, dateOfBirth, caste, education, occupation, profession, jobAlerts, jobTraining, address: { street, city, district } })

member.userId = user._id
user.roles = 'member',
member.verificationStatus = true
member.referredBy = volunteer
Promise.all([member.save(), user.save()]).then(member => {
res.send({
notice: 'Successfully registered',
member,
status: 201
})
})
} else {
res.send("User already exists")
}
})

}
} catch (error) {
res.send({
status: 404,
error: error.message
})
}
})

我希望将家庭成员数据推送到子文档中,每次我循环遍历 re.body 并创建一个对象并将数据推送到数据库中,但它没有发生

最佳答案

body.familyMembers.forEach((familyMember) => {

使用带有回调的异步方式迭代速度太快,导致循环等待 User.findOne 首先完成执行。

如果您在查询执行时控制台数组,那么它显示到目前为止它迭代了太多条目

关于node.js - 如何将对象数组推送到子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55514500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com