It seems that there is a null value being returned in the midst of the

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;

Answer №1

When working with a specific seat object, remember to place the methods on Seat.prototype rather than directly on the Seat itself.

It's important to keep in mind that calling result(...) multiple times can lead to issues if the function is not properly terminated. This could cause problems for any code expecting the callback to be executed only once.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing AJAX to dynamically create graphs using Highcharts

I'm having trouble integrating AJAX with highcharts in my project. Here is a snippet of my HTML and javascript code: function test () { var options = { chart : { renderTo : 'container', type : 'spline', ...

Node/Express: Retrieve the req.query parameter from the URL and convert it into a UNIX timestamp

Here is the URL that I am working with: In my Express app.get function, I am trying to extract 2015-12-25 from the query string and return JSON data containing a UNIX timestamp and a UTC timestamp. The structure of the JSON should be as follows: {unix: & ...

In Angular/Typescript, dynamically add a class to a `td` element when it is clicked. Toggle the class on and off

My problem arises when trying to individually control the arrow icons for each column in my COVID-19 data table. By using Angular methods, I aim to show ascending and descending arrows upon sorting but run into the challenge of changing arrows across all c ...

When employing the updateOne method within a mapping function, specify the filter using the unique identifier, set the document using $set, and enable upsert as true. Despite this configuration, a new document

Here is the code snippet in the controller where I fetch records from my Google Calendar API and pass the data to a function. Inside that function, the code for inserting documents (records) appears as follows: Holiday.bulkWrite( holidays.map((holida ...

What strategies can be used to manage Error return types in Typescript?

I have a situation where I need to handle either an object of type Person or an Error being returned by a function. My goal is to read the values of keys in the returned object only if it's not an Error. The code snippet below throws an error on the ...

Error message in Linux for NodeJS global NPM package: "File or directory does not exist"

After setting up my Ubuntu 14.04 system, I installed nodejs and npm using the command: sudo apt-get install nodejs npm To ensure packages utilize the node interpreter instead of nodejs, I created a symlink with: sudo ln -s /usr/bin/nodejs /usr/local/bin ...

Troubleshooting an Express Server - Why can't I /GET?

I'm working on a simple app to practice using Express, but I'm encountering trouble with the setup. Whenever I try to run the app, I keep getting a Cannot /GET error message. Here is an overview of the code: In the main directory - var express ...

Sending data to a function that is generated dynamically within a loop

How can I ensure that the date2Handler is triggered with the corresponding date1 and date2 values from the month in which the date1 change occurred? Currently, whenever the date1 value is changed in any month, the date2Handler is called with the "month" t ...

How do you switch selection to "hold" mode using Javascript?

In my Markdown preview area, clicking on text will cause the preview area to switch to a markdown source editor automatically, with the cursor jumping to the position corresponding to where it was clicked. function onMouseDown(e) { const range = documen ...

PNG file is not displayed on React TSX page despite absence of any errors

I've implemented this initial design template from GitHub (https://github.com/vikpe/react-webpack-typescript-starter). Additionally, I'm utilizing react-bootstrap and have a container that includes a backgroundImage. const heroImage = require(&q ...

The HTML 5 video is not functioning properly on an iPad, and the layout of the iPad has black areas on the sides

Having some trouble with an HTML 5 project where the video plays on Chrome and Safari PC browsers but not on iPad. The task at hand is to get it working in portrait mode only, with the video playing when tapped/clicked. <!doctype html> <!--[if ...

The challenge with the Optional Chaining operator in Typescript 3.7@beta

When attempting to utilize the Typescript optional chaining operator, I encountered the following exception: index.ts:6:1 - error TS2779: The left-hand side of an assignment expression may not be an optional property access. Here is my sample code: const ...

Utilizing a filter within the ng-model directive

I have a question about using a filter with an h3 element. Here is the code snippet: {{ event.date | date:'dd-MM-yyyy' }} It's working perfectly fine and Angular is formatting the date as expected. However, when I try to use the same filte ...

"pre and post" historical context

Is there a way to create a stunning "Before and After" effect using full-sized background images? It would be amazing if I could achieve this! I've been experimenting with different examples but can't seem to get the second 'reveal' di ...

Is it possible to use Vuelidate for password validation in Vue.js?

I found a helpful reference on How to validate password with Vuelidate? validations: { user: { password: { required, containsUppercase: function(value) { return /[A-Z]/.test(value) }, containsLowercase: fu ...

Unable to transfer the properties of reactjs to react-chartist

How can I pass the state from the parent component "main.js" into the child component "bar.js"? //main.js import React, { Component } from 'react'; import BarChart from './Bar-chart'; class Hero extends Component { cons ...

"Upon refreshing the browser, an Angular map loses its positioning and appears blank

I have implemented a LeafLet map in my Laravel 9 system, which is functioning properly with some exceptions. Here's how it looks like: https://i.stack.imgur.com/kcuVr.jpg The code in the controller (CompetitionsMapController.php) is as follows: ...

When making a JQuery - Ajax get request, I did not receive the "extracts" or "exintro" (summary) property in the response

Lately, I've been working on a small web application that displays search results from Wikipedia on the webpage after entering a search term into a text field. This has been a project that I’ve dedicated a lot of time to. I have configured an ajax g ...

Guide on mapping in node.js utilizing a database object that is returned with a specific where clause

Just starting to work with Node and I encountered this issue when I added a where clause to my query... Error: Cannot read property 'map' of undefined I'm having trouble using the map function and combining data. The query is giving me the ...

Unable to install react-dom/test-utils using npm

I recently included two libraries in my package.json "devDependencies": { ... "react-dom/test-utils": "*", "react-test-renderer/shallow": "*" }, These were recommended by the React documentation to align with version 16 of the React ecosy ...