score:0
So here is a possible solution that I you could explore(I am sure there surely is more solutions but currently when writing this, this is the solution I can think of).
- So I am assuming maybe you have user model and lets say the models is looking somewhat like this below:
const userSchema = mongoose.Schema({
username:{type:String, required:true},
email:{type:String, required:true, email},
password:{type:String, required:true}
})
const userModel = mongoose.model('userModel', userSchema);
- So we have our user models all set and we uniquely can indentify a user by the _id given by mongoose or email that we set as unique.
So now we need a plane trip model:
const planeTripSchema = mongoose.Schema({
from:{type:String, required:true},
to:{type:String, required:true},
seats:{type:Array, required:true}
});
const planeTrip = mongoose.model('planeTrip', planeTripSchema);
- Now we have a plane trip model.
- So now how I can you handle that you have different seats within your array of seat
Implementation when creating plane trip to set up unique value to identify each sit:
- So first I would install uuid
npm i uuid
oryarn add uuid
- So now this is dependent on how many people this flight can take lets assume that it takes 10 people.
So this way might not be the most efficient solution but this what I am thinking currently when writing this
So now you aim to have 10 seats set before you can save the flight trip
So then what you do is you can have 10 variables seat1-seat10(This is where this solution become inefficient) and call he
uuid
lib to get unique identifier(will give back unique number)Then below is how you then instantiate new flight
const newFlight = new planeTrip({
from:'New York',
to:'Washington DC',
seats:[
{
occupied:false
seat_id:<use the variable you made when using uuid for seat1-10
seat_number:1 // You can then populate each sit number self from 1-10 on each entry but id you'll get fom uuid
person:"",
}
//So you can do this 10 time because plane supports 10 seats in this exampl
]
})
- So now when you save the document to mongoDB you have seats that you can uniquely identify
So now how then do you handle occupied sits:
So now in client side when you make a request to get flight sit details you will use javaScript map to map through seats array and then check if occupied === true as we wrote there above
So it's true then you can leave as is
Else if it false you can use CSS to then underline over the seat number
Meaning that your css class must also some ternary operations
And a normal user definitely will know that it's occupied
Then when user clicks on the available sit you then sit through the sit number and sit id that would be available easily when you mapping through the seats array
Not forgetting you also have to send through the email of the user because in our example here we uniquely identify our users with email
Then at the back-end only thing you do is update ocupied of the sit ID that you got from client from false to true
You also update the person from empty string to email received from client
So this is a possible solution that can be improved if you sit down and think it through but at least I gave you a starting point of which direction to take but I believe this solution can be improved to being more efficient than this
Source: stackoverflow.com
Related Query
- Updating lists on FrontEnd from MongoDB using mongoose and react
- Updating documents of mongodb using react nodejs and ajax
- Selecting random item from Javascript .filter() using React and Mongoose
- Updating and merging state object using React useState() hook
- How to prevent child component from re-rendering when using React hooks and memo?
- CDN links for React packages and how to import it when using react using the scripts from CDN
- generating pdf from Html in React using html2canvas and jspdf
- How to execute store.unsubscribe from useEffect using React hooks and Redux
- Remove .html extension from a url for a website built using React and Webpack
- React 16: Call children's function from parent when using hooks and functional component
- Access stores from class using mobX and react Context
- React updating progress from a compute intensive function using setState
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- how to put backend and frontend together - returning react frontend from fastapi backend endpoint
- Getting the ref from a dynamic component when using Redux, React and react-router-dom 4.x
- Importing and using component from custom React Component Library results in Invariant Violation: Invalid hook call
- React input onchange simulation not updating value using Jest and enzyme
- Download file from Express API using React and Axios
- Rendering from a child to a parent using refs and portals in React
- Delete node from Firebase using React and firereact
- How to make my component re render after updating props from selector in react and redux-saga?
- How to send data from react and use the request body in express, using Axios?
- React Leaflet: Get objects from MongoDB to Leaflet map as markers (using node.js and express)
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- How to display an image with <img> from Mongoose using React front-end
- setState from an API response using superagent and React
- How to download pdf from puppeteer using Nest js as Server Side and React in Client Side?
- React How to change state value from child to Parent using useState and useContext
- How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js?
- Updating Data in Firebase using React and Axios
More Query from same tag
- Losing a part of my state in Redux Toolkit
- Retrieving value from onChange option dropdown with useState and fetch request
- setInterval does not work properly in ReactJS
- ReactJS: Which method is best to store parameters of a HTML canvas element, referenced through useRef?
- Facebook's react.js -- object is not a function
- Meteor - rendering a blaze template within a react template?
- How could I reset the state of my component according to changes in the parent component?
- Is there a way to pass state to pages in Gatsby?
- How to trigger images to load in background using Next.js
- Why redux toolkit boolean toggle doesn't work?
- How to handle request-in-flight case to show loader using redux saga?
- Reusable search component in react - css failing on overflow of text
- Cannot fetch data after refreshing the page in Next.js
- Mock Response.headers.get()
- material ui datagrid a status menu in table cell
- Mock addEventListener for google maps
- How can I reset a piece of Redux, but maintain the rest?
- Nextjs - Reactjs - Links patterns - Invariant Violation: React.Children.only expected to receive a single React element child
- how to set onChange handler to change the language in react i18next
- Custom status change events in Javascript
- Reactjs + D3 v4 projection issue
- how to fetch a nested resource for every component in React router dom route
- remove hash from react-router-dom url
- When trying to create new item in MERN stack, getting TypeError: this.props.meals.map is not a function
- Docker - served react app, asset-manifest.json with incorrect filenames
- reactjs, get state and passing it as param without changing it
- My query is failing in relay and I don't know why?
- React: Update variable when url changes with useEffect, react-router
- How can I refactor my arrow functions to use an if statement?
- React Axios fetch response with error Objects are not valid as a React child