I have developed a model using express. While setting this
in one function, it returns null in another function, forcing me to use return
. What is the proper method to handle this situation?
const Seat = function(seat) {
this.seat = seat.seat;
this.seatNumber = seat.seatNumber;
this.price = seat.price;
this.available = seat.available;
this.disabilityAccessible = seat.disabilityAccessible;
};
Seat.findSeat = (key, value) => {
this.seat = file.find(obj => obj[key] === value);
return this.seat;
};
Seat.bookSeat = function(seatNumber, result) {
this.seat = this.findSeat('seatNumber', seatNumber);
if (this.seat === undefined || Object.keys(this.seat).length === 0) {
result({ error: true, message: "Seat not found" });
}
if(!this.seat.available){
result({ error: true, message: "Seat unavailable for booking" });
}
const newSeatData = file.map(row => {
if (row.seatNumber === seatNumber) {
row.available = false;
this.seat = row;
}
return row;
});
fs.writeFileSync("./model/seatData.json", JSON.stringify(newSeatData,null,2), "utf-8", function(err) {
if (err) {
result({ error: true, message: "Failed to update booking" });
}
});
result(null, this.seat);
};
module.exports = Seat;