【发布时间】:2018-01-13 07:13:18
【问题描述】:
我对 google firebase 和云功能非常陌生。 我只是打了个招呼:
- 连接到 Cloud firestore[beta],其中包含超过 100,000 条记录
- 检索前 1 条记录。
代码如下:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
console.log(mydump(functions.config().firebase));
admin.initializeApp(functions.config().firebase);
exports.helloWorld = functions.https.onRequest((request, response) => {
const tokens = [];
const html = [];
var oldItemsQuery = admin.database().ref();
html.push("==============begin========================");
return oldItemsQuery.once('value').then(snapshot => {
console.log("once value, snapshot key:" + snapshot.key
+",val:" +snapshot.val()
+",hasChild/profile:" +snapshot.hasChild("profile")
+",hasChild/everyday:" +snapshot.hasChild("everyday")
+",numChildren:" +snapshot.numChildren()
+",toJSON:" +snapshot.toJSON()
);
html.push("============end===================");
response.send(html.join("<br>\n"));
});
});
我相信这段代码非常简单。但是当我在浏览器中访问 url 时,结果并不像预期的那样。 https://us-central1-xxxxxxxxx.cloudfunctions.net/helloWorld?r=125
我在控制台收到消息
once value, snapshot key:null,val:null,hasChild/profile:false,hasChild/everyday:false,numChildren:0,toJSON:null
这意味着我的数据库中没有记录。我尝试了以下代码,但结果是一样的。
var oldItemsQuery = admin.database().ref("");
var oldItemsQuery = functions.database.ref('/{playerId}/profile').orderByChild('timezone').limitToFirst(1);
//var oldItemsQuery = admin.database().ref('/{playerId}/profile').orderByChild('timezone').limitToFirst(1);
谢谢。我花了 3 天时间,但可以解决。
【问题讨论】:
-
admin.database.enableLogging(true);我在脚本顶部添加该行并获取控制台消息。看来一切正常
-
你能发布你的数据库结构吗?
-
您的代码连接到 Firebase 实时数据库。但是在您的描述中,您说您连接到 Cloud Firestore。这两个是不同的数据库。您将记录添加到哪一个?
-
你好,弗兰克,我想你明白了。我相信我正在使用 Cloud Firestore [beta]。那么,如果我想在我的 Cloud 功能中访问 Cloud Firestore [beta],应该使用哪些连接代码?我会尝试:const admin = require('firebase-admin');常量函数 = 要求('firebase-functions'); admin.initializeApp(functions.config().firebase); var db = admin.firestore();
-
你好罗萨里奥,谢谢你的回复。架构退出简单/userid001; /userid001/profile;就是这样
标签: javascript firebase firebase-realtime-database google-cloud-functions