首页 > 解决方案 > 通过实时数据库 ServerValue 递增崩溃的云函数

问题描述

我是 Firebase 实时数据库的新手,但到目前为止,我已经使用 Firestore 几个月了。我在下面编写了一个云函数,它尝试使用 ServerValue 功能增加实时数据库中特定记录的访问计数。但它与 Firebase 日志一起崩溃

8:01:29.257 am incrCountByRTDB Function execution started
8:01:29.257 am incrCountByRTDB Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
8:01:29.280 am incrCountByRTDB incrPersonAccessCountBy1
8:01:29.862 am incrCountByRTDB Function execution took 605 ms, finished with status: 'crash'

代码片段

const admin = require('firebase-admin');
const functions = require('firebase-functions');

admin.initializeApp();

exports.incrCountByRTDB = functions.https.onRequest((req, res) => {
  console.log("incrPersonAccessCountBy1");
  admin.database().ref('Persons').child('mur_123').child('accessCount')
  .set(admin.database.ServerValue.increment(1))
  .then(function() {
    console.log('Increment succeeded');
    return res.status(200).send("Successfully incremented the accesscount");
  }).catch(function(error) {
    console.log('Increment failed', error);
    return res.status(200).send("Increment failed");
  })
});

exports.createPersonsCollection = functions.https.onRequest((req, res) => {
  admin.database().ref("Persons").child('mur_123').set({
    name: 'murali',
    accessCount: 0
}).then(function(){
  return res.status(200).send("Successfully created the persons collection");
}).catch(function (ex){
  console.log("Error", ex);
  return res.status(500).send("Creation failed");
})
});

如果我遗漏了什么,请告诉我并帮助我解决这个问题。

谢谢穆拉利

标签: javascriptnode.jsfirebasefirebase-realtime-databasegoogle-cloud-functions

解决方案


推荐阅读