使用node.js将数据设置到firebase数据库后,'then'回调出错

问题描述 投票:1回答:1

我有这个代码

app.get('/updateSaving/:name/:amount' , function(req,res){
   console.log('sending request update saving');
   var d = new Date();
   date = d.toString();
   firebase.database().ref('saving/'+ req.params.name).set({
     name : req.params.name,
     amount: req.params.amount,
     last_date : date
}).then(function(res){
    console.log('Saving Updated');
    res.json({ success: 'true' });
 });
})

在我调用此代码之后,firebase设置了记录,但为什么我的回调res.json({ success: 'true' });出错? 这是错误UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): TypeError: Cannot read property 'json' of undefined

node.js firebase-realtime-database callback promise
1个回答
1
投票

Reference.set() method returns a void promise。你在你的res处理程序中声明了then(),该处理程序隐藏了res中的app.get()

解决方案是删除res解析处理程序中的set()

app.get('/updateSaving/:name/:amount' , function(req,res){
   console.log('sending request update saving');
   var d = new Date();
   date = d.toString();
   firebase.database().ref('saving/'+ req.params.name).set({
     name : req.params.name,
     amount: req.params.amount,
     last_date : date
}).then(function(){
    console.log('Saving Updated');
    res.json({ success: 'true' });
 });
})
© www.soinside.com 2019 - 2024. All rights reserved.