1
votes

@firebase/database: FIREBASE WARNING: Exception was thrown by user callback. Error: Can't set headers after they are sent. at ServerResponse.setHeader (_http_outgoing.js:371:11) at ServerResponse.header (C:\Users\G I E\node_modules\express\lib\response.js:725:10) at ServerResponse.send (C:\Users\G I E\node_modules\express\lib\response.js:170:12) at ServerResponse.json (C:\Users\G I E\node_modules\express\lib\response.js:256:15) at C:\Users\G I E\Desktop\firebase\app\server.js:47:13 at C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\view\EventRegistration.js:65:22 at Object.exports.exceptionGuard (C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\util\util.js:536:9) at EventList.raise (C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\view\EventQueue.js:158:24) at EventQueue.raiseQueuedEventsMatchingPredicate_ (C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\view\EventQueue.js:111:41) at EventQueue.raiseEventsForChangedPath (C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\view\EventQueue.js:95:14) C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\util\util.js:547 throw e; ^ Error: Can't set headers after they are sent. at ServerResponse.setHeader (_http_outgoing.js:371:11) at ServerResponse.header (C:\Users\G I E\node_modules\express\lib\response.js:725:10) at ServerResponse.send (C:\Users\G I E\node_modules\express\lib\response.js:170:12) at ServerResponse.json (C:\Users\G I E\node_modules\express\lib\response.js:256:15) at C:\Users\G I E\Desktop\firebase\app\server.js:47:13 at C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\view\EventRegistration.js:65:22 at Object.exports.exceptionGuard (C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\util\util.js:536:9) at EventList.raise (C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\view\EventQueue.js:158:24) at EventQueue.raiseQueuedEventsMatchingPredicate_ (C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\view\EventQueue.js:111:41) at EventQueue.raiseEventsForChangedPath (C:\Users\G I E\Desktop\firebase\node_modules\@firebase\database\dist\cjs\src\core\view\EventQueue.js:95:14)

I am just trying to save data to firebase, after submitting the form the error shown in the console

that's my function code

app.post('/AddNewCountry',function(req,res){

   var cid = firebase.database().ref().child('country').push().key;
   var data = {
    country_id: cid,
    country_name: req.body.name,
    country_flag: req.body.img
   } 
   var updates = {};
   updates['/country/' + cid] = data;
   firebase.database().ref().update(updates);
   console.log('The country is created successfully!');
})

Any help ?

1
After console.log, can you add this res.send({ status: 'successfull' })? - dnp1204

1 Answers

0
votes

It is looking quite correct, unless if req.body.name or req.body.img boths should not be undefined while saving to the database. So before to save, check the values :

app.post('/AddNewCountry',function(req,res){

   var cid = firebase.database().ref().child('country').push().key;
   var data = {
    country_id: cid,
    country_name: req.body.name,
    country_flag: req.body.img
   } 
  if (req.body.name && req.body.img) {
   var updates = {};
   updates['/country/' + cid] = data;
   firebase.database().ref().update(updates);
   console.log('The country is created successfully!');
  }
})