Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
I want to preserve backend data on reload

I have created an app that has a cart that sends data to the backend, now when I restore I want to preserve the data and display the same data as before.

I have used the PUT method instead of POST and when I send and get data from Firebase, data is preserved on reload and the data from the database is visible, but if I use my own backend in Node.js, I am not able to get the data on reload. This is where I am fetching data from the backend.

export const fetchCartData=()=>{
 return async(dispatch)=>{
  const getData= async() =>{
   const response = await fetch('https://localhost:5000/');
   if(!response.ok){
    throw new Error('Something went wrong'); 
   }
   const info=await response.json();
   return info;
};

  try{
   const data= await getData();
   console.log("Here is data from Firebase", data);
   dispatch(order_action.replaceCart({
     items:data.items || [],
     totalQuantity:data.totalQuantity || 0
   }));
  }catch(error){
   dispatch(show_action.showNotification({
     status:"failed",
     title:"Error...",
     message:"Some error occurred."
   }));
  }
 }
}

Tha backend Code is:

const express=require("express");
const bodyParser=require('body-parser');
const cors=require('cors');
const app=express();
app.use(cors());
app.use(bodyParser.json());

app.put("/",function (req,res) {
  const data={
    items:req.body.items,
    totalQuantity:req.body.totalQuantity
  }
console.log(data);
console.log("END");
res.send({data});
})

app.get("/",function (req,res) {
console.log(req.body);
const data={
  items:req.body.items,
  totalQuantity:req.body.totalQuantity
}

res.send({data})
})

app.listen(5000,function () {
console.log("Running on 5000");
})
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use localStorage on the browser i.e at the client-side. Whenever there is any change in the cart do these steps:

  1. Send data to the backend API using the PUT method and store it in DB or cache(based on the website and users you are dealing with).

  2. Once you get the response from API, update your localStorage data.

localStorage.set('cart', JSON.stringify(dataFromAPI));

Now, on every reload you will always be getting the last updated cart data.

when I send and get data from Firebase, data is preserved on reload and the data from the database is visible

Just for your knowledge, firebase is a database and when you save data, it is persistent. Now, on reload you must be calling the firebase DB to get the data back that's why you see the data on the client otherwise it is impossible to get data without caching or saving it locally.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!