Here is code to update db.
let profileUrl = 'example'
UserSchemaModel.findOneAndUpdate({_id:userId}, {$set: {profileUrl:profileUrl} }, {new:true})
.then((updatedUser:UserModel) => {
console.log(updatedUser.profileUrl)
})
result:undefined
As you can see in the result, profileUrl field is not updated. so I have inserted overwrite option.
But it didn't work. Following error is appeared.
src/controllers/user.ts(267,106): error TS2345: Argument of type '{ new: true; overwrite: boolean; }' is not assignable to parameter of type 'ModelFindOneAndUpdateOptions'.
Object literal may only specify known properties, and 'overwrite' does not exist in type 'ModelFindOneAndUpdateOptions'.
UserSchema is same to following.
{
name:String,
thumbnailImg:String,
email:String,
phone:Number,
profileUrl:String
}
now saved data is same following.
[{
_id:ObjectId("5ffe07805cbab00019cdbbcd"),
name:"Jhon",
thumbnailImg:"thumbnail_image_url",
email:"jhon@gmail.com",
phone:"1212121212",
}]
I have tried several times, but the result is same.
How can I fix this issue?
I think there are methods to fix this issue.
You are trying to match using the _id field which is defined as ObjectId type in your schema.
So in your query, try searching using the ObjectId type.
let ObjectId = require('mongoose').Types.ObjectId;
let profileUrl = 'example'
UserSchemaModel.findOneAndUpdate({_id: new ObjectId(userId)}, {
$set: {
profileUrl: profileUrl
}
}, {new:true}).then((updatedUser:UserModel) => {
console.log(updatedUser.profileUrl)
})