[英]How can i get data snapshot and then update database in firebase cloud functions http request
我正在嘗試在雲功能的HTTPS請求中獲取Firebase實時數據庫的數據快照,然后將來自查詢的值添加到快照值,然后再次將其設置為數據庫。
這是我的代碼。
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 warning Unexpected function expression prefer-arrow-callback
是警告,表示您應該使用ES6箭頭函數語法而不是帶有“ function ”一詞的老式語法:
return admin.database().ref('/Users/' + userId).once('value').then( snapshot => {
然后是實際的錯誤...
18:70 error Each then() should return a value or throw promise/always-return
告訴您,每次使用.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;
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.