Below is the code snippet for implementing the PUT method:
[/api/[id].ts]
case "PUT":
try {
const user = await UserModel.findOneAndUpdate(
{
_id: id,
},
{
$set: req.body,
}
);
if (!user) {
return res.status(400).json({ success: false });
}
res.status(200).json(user);
} catch (error) {
res.status(400).json({ success: false });
}
break;
https://i.stack.imgur.com/GpG2u.png
The code does not update the money
field as expected. However, changing the email
field seems to work just fine.
Here is the User model used in this implementation:
[models/UserModel.ts]
import { model, models, Schema, SchemaTypes } from "mongoose";
const UserSchema = new Schema({
name: {
type: SchemaTypes.String,
required: true,
},
email: {
type: SchemaTypes.Number,
required: false,
},
image: {
type: SchemaTypes.String,
required: true,
},
emailVerified: {
type: SchemaTypes.Number,
required: false,
},
money: {
type: SchemaTypes.Number,
required: true,
default: 100,
},
luck: {
type: SchemaTypes.Number,
required: true,
default: 1,
},
});
export default models.User || model("User", UserSchema);