I'm new to mongodb and mongoose.
Please help me out. I'm stuck in this problem that I describe below.
This is my code. addressSchema and another schema for saving user id and the address
when i try to update the document im not getting any response from the below code. but when i try 'find()' im getting the response.
const addressSchema = mongoose.Schema({
name: {
type: String,
required: true,
trim: true,
min: 2,
max: 40
},
mobileNo: {
type: String,
required: true,
trim: true
},
pincode: {
type: String,
required: true,
trim: true
},
landmark: {
type: String,
required: true,
trim: true
},
address: {
type: String,
required: true,
trim: true
},
area: {
type: String,
required: true,
trim: true
},
state: {
type: String,
required: true,
trim: true
},
alternatePhoneNo: {
type: String
},
addressType: {
type: String,
required: true,
enum: [ 'home', 'work' ]
}
})
Schema for storing the address array and user ID
const userAddressSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
address: [addressSchema]
}, { timestamps: true })
I tried updating many ways but still cant update
//Code to update the document
const editAddress = (req, res) => {
const { address } = req.body
if(address) {
UserAddress.findOne({ user: req.user._id })
.exec((error, _userAddress) => {
if(error) return res.status(400).json({ error })
if(_userAddress) {
UserAddress.findOneAndUpdate({ "_id": _userAddress._id,"address._id": address._id }, {
"$set": {
"address.$": {
"name": address.name,
"mobileNo": address.mobileNo,
"pincode": address.pincode,
"landmark": address.landmark,
"address": address.address,
"area": address.area,
"state": address.state,
"alternatePhoneNo": address.alternatePhoneNo,
"addressType": address.addressType
}
}
})
.exec((error, data) => {
if(error) return res.status(400).json({ error })
if(data) return res.status(201).json({ data })
console.log(error)
console.log(data)
})
}
})
}
}
Guys please help Thanks in advance
Try awaiting
the .findOneAndUpdate()
function:
const editAddress = async (req, res) => {
const { address } = req.body
if(address) {
await UserAddress.findOne({ user: req.user._id })
.exec((error, _userAddress) => {
if(error) return res.status(400).json({ error })
if(_userAddress) {
await UserAddress.findOneAndUpdate({ "_id": _userAddress._id,"address._id": address._id }, {
"$set": {
"address.$": {
"name": address.name,
"mobileNo": address.mobileNo,
"pincode": address.pincode,
"landmark": address.landmark,
"address": address.address,
"area": address.area,
"state": address.state,
"alternatePhoneNo": address.alternatePhoneNo,
"addressType": address.addressType
}
}
})
.exec((error, data) => {
if(error) return res.status(400).json({ error })
if(data) return res.status(201).json({ data })
console.log(error)
console.log(data)
})
}
})
}
}
Awaiting
this fixed my error too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.