【问题标题】:firebase cloud function Retrieve datafirebase 云功能 检索数据
【发布时间】:2018-02-20 15:16:59
【问题描述】:

当我尝试运行这个函数时,它会说:

错误:禁止

您的客户端无权从此服务器获取 URL /tdata。

我的网址是https://us-central1-*****.cloudfunctions.net/sdata

代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
 exports.tdata = functions.database.ref('/journey/kanpur-allahabad/seat').onWrite(event => {
    const num = event.data.val()
    
  });

【问题讨论】:

    标签: firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

     exports.tdata = functions.https.onRequest((req, res) => {
    	  var db = admin.database();
    var ref = db.ref("/journey/kanpur-allahabad/seat");
    	  ref.on("value", function(snapshot) {
      console.log(snapshot.val());
      }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    });
      });

    【讨论】:

      【解决方案2】:

      您声明了一个由写入数据库的数据触发的函数。您不能通过 URL 调用该函数,这仅适用于 HTTP 触发的函数。

      如果您想在本地测试数据库触发的函数,您可以trigger it through the Cloud Functions shell

      【讨论】:

      • 感谢您的意见,我使用 admin sdk 解决了购买问题。代码是exports.tdata=functions.https.onRequest((req, res) => { var db = admin.database(); var ref = db.ref("/journey/kanpur-allahabad/seat"); ref .on("value", function(snapshot) { console.log(snapshot.val()); }, function (errorObject) { console.log("读取失败:" + errorObject.code); }); } );
      猜你喜欢
      • 2018-02-01
      • 2017-10-28
      • 2018-12-04
      • 1970-01-01
      • 2019-01-03
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      相关资源
      最近更新 更多