@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)
我只是想将数据保存到 firebase,提交表单后控制台中显示错误
这是我的函数代码
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!');
})
有什么帮助吗?
请您参考如下方法:
它看起来非常正确,除非在保存到数据库时req.body.name
或req.body.img
两者都不应该是未定义
。因此,在保存之前,请检查这些值:
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!');
}
})