Javascript 如何在firebase cloud functions http请求中获取数据快照并更新数据库
我正在尝试在云函数的HTTPS请求中获取firebase实时数据库的数据shapshot,然后将来自查询的值添加到快照值,然后再次将其设置为数据库 这是我的密码Javascript 如何在firebase cloud functions http请求中获取数据快照并更新数据库,javascript,firebase,google-cloud-functions,firebase-admin,Javascript,Firebase,Google Cloud Functions,Firebase Admin,我正在尝试在云函数的HTTPS请求中获取firebase实时数据库的数据shapshot,然后将来自查询的值添加到快照值,然后再次将其设置为数据库 这是我的密码 exports.addCredits = functions.https.onRequest((req, res)=>{ console.log(req.query.UserID); var credits = req.query.amount var userId = req.query.UserID
exports.addCredits = functions.https.onRequest((req, res)=>{
console.log(req.query.UserID);
var credits = req.query.amount
var userId = req.query.UserID
return admin.database().ref('/Users/' + userId).once('value').then(function(snapshot) {
var userPoints = snapshot.val().Credit
const databaseRef = admin.database().ref("Users").child(userId+"/Credit")
res.send("Your Credits "+ credits + " And User ID " + userId + " user points" + userPoints);
var total = credits + userPoints
databaseRef.set(total);
})
})
部署代码时,终端出现错误
18:70 warning Unexpected function expression prefer-arrow-callback
18:70 error Each then() should return a value or throw promise/always-return
如何获取数据库的快照并再次写入?这些错误消息非常有用,Ganesh,请阅读这两条消息
18:70警告意外函数表达式首选箭头回调
是一个警告,指出您应该使用ES6 arrow函数语法,而不是带有“function”一词的老式语法:
返回admin.database().ref('/Users/'+userId)。一次('value')。然后(快照=>{
然后是实际的误差
18:70错误每个then()应返回一个值或抛出承诺/始终返回
告诉您每次使用.then()
,内部函数都需要返回一些内容
return admin.database().ref('/Users/' + userId).once('value').then( snapshot => {
var userPoints = snapshot.val().Credit
const databaseRef = admin.database().ref("Users").child(userId+"/Credit")
res.send("Your Credits "+ credits + " And User ID " + userId + " user points" + userPoints);
var total = credits + userPoints
databaseRef.set(total);
// You are inside of a .then() block here...
// you HAVE return SOMETHING...
// if you want, you could do: return databaseRef.set(total);
// or even just: return true;
})
这些错误消息非常有用,请阅读它们
18:70警告意外函数表达式首选箭头回调
是一个警告,指出您应该使用ES6 arrow函数语法,而不是带有“function”一词的老式语法:
返回admin.database().ref('/Users/'+userId)。一次('value')。然后(快照=>{
然后是实际的误差
18:70错误每个then()应返回一个值或抛出承诺/始终返回
告诉您每次使用.then()
,内部函数都需要返回一些内容
return admin.database().ref('/Users/' + userId).once('value').then( snapshot => {
var userPoints = snapshot.val().Credit
const databaseRef = admin.database().ref("Users").child(userId+"/Credit")
res.send("Your Credits "+ credits + " And User ID " + userId + " user points" + userPoints);
var total = credits + userPoints
databaseRef.set(total);
// You are inside of a .then() block here...
// you HAVE return SOMETHING...
// if you want, you could do: return databaseRef.set(total);
// or even just: return true;
})
在这种情况下,您应该返回
databaseRef.set(总计)的结果;
,因为这是一个异步操作。如果您不返回在该操作完成时解析的承诺,则运行函数的容器可能会在集
完成之前终止代码。在这种情况下,您应该返回数据库引用集的结果(总计);
,因为这是一个异步操作。如果在该操作完成时不返回解析的承诺,则运行函数的容器可能会在集合完成之前终止代码。