In the update
method of my NestJS UserService
, I'm using updateOne
to update a user's name by their ID. However, the update doesn't seem to be working properly. What am I doing wrong?
Here's the relevant code:
async update(id: ObjectId, updateUserDto: UpdateUserDto) {
return await this.userModel.updateOne({
_id: id,
name: {
firstName: updateUserDto.name.firstName,
lastName: updateUserDto.name.lastName,
},
});
}
Thanks for helping me out!
When I call this method and pass in a user ID and a UpdateUserDto object containing a new first name and/or last name, I expect the user's name to be updated in the database. However, when I check the database after calling the update method, the name hasn't changed.
According to docs the correct function call should have 2 arguments: 1st for filter, 2nd for updated fields.
async update(id: ObjectId, updateUserDto: UpdateUserDto) {
return await this.userModel.updateOne({_id: id,}, {
name: {
firstName: updateUserDto.name.firstName,
lastName: updateUserDto.name.lastName,
},
});
}