I’m trying to update a document in my event schema when a successful order is placed. I want to make ‘isPurchased’ to true so the product can no longer be purchased but when i set it it stays as false. What am i doing wrong here?
Here is the action
export const createOrder = async (order: CreateOrderParams) => {
try {
await connectToDatabase();
const eventId = order.eventId
const updatedEvent = await Event.findOneAndUpdate(
{eventId},
{ $set: { isPurchased: true } },
{new:true}
)
const newOrder = await Order.create({
...order,
event: order.eventId,
buyer: order.buyerId,
});
console.log(updatedEvent)
return {
order: JSON.parse(JSON.stringify(newOrder)),
updated: JSON.parse(JSON.stringify(updatedEvent))
}
} catch (error) {
handleError(error);
}
}
here is the schema for the ‘Event’ model
import { Document, Schema, model, models } from "mongoose";
export interface IEvent extends Document {
_id: string;
title: string;
seatInfo: string;
location: string;
createdAt: Date;
startDateTime: Date;
ticketUrl:string;
price: string;
quantity: string;
seller: { _id: string, firstName: string, lastName: string };
isPurchased:boolean;
}
const EventSchema = new Schema({
title: { type: String, required: true },
seatInfo: { type: String },
location: { type: String },
createdAt: { type: Date, default: Date.now },
startDateTime: { type: Date, default: Date.now },
ticketUrl: {type: String },
price: { type: String },
quantity: {type: String },
seller: { type: Schema.Types.ObjectId, ref: 'User' },
isPurchased: {type:Boolean, default:false}
})
const Event = models.Event || model('Event', EventSchema);
export default Event;
finally here are the params used in the action
export type CreateOrderParams = {
stripeId: string
eventId: string
buyerId: string
totalAmount: string
createdAt: Date
}
I tried the findOneAndUpdate as well as findByIdAndUpdate but it is not changing isPurchased to true. It is only creating the order which is fine, and creating the order in the MongoDB Database.
user24689160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.