Firebase 数据库查询为所有端点返回相同的数据
Firebase database query returning same data for all endpoints
我尝试创建一个 firebase 函数,用于在将新 post 插入 Firebase 实时数据库时向所有设备发送推送通知。我的设备令牌位于 profile-notifications 端点下。当我尝试通过向数据库插入一个新的 post 来执行我的函数时,我的反应是出乎意料的。我阅读了来自 https://firebase.google.com/docs/functions/ and https://firebase.google.com/docs/reference/admin/node/admin.database 的文档,但无法进一步了解。这是我的日志:
5:23:47.451 PM debug sendNewPostNotification
Function execution took 1145 ms, finished with status: 'ok'
5:23:47.447 PM info sendNewPostNotification
p:0: from server: {"r":4,"b":{"s":"ok","d":""}}
5:23:47.446 PM info sendNewPostNotification
p:0: from server: {"r":3,"b":{"s":"ok","d":{}}}
5:23:47.444 PM info sendNewPostNotification
Result: T { A: P { k: Sb { Ka: [Function: vb], ba: [Object] }, aa: P { k: [Object], aa: null, wb: [Object], Bb: '' }, wb: Zc { ld: [Object], ac: [Object] }, Bb: null }, V: R { u: Gd { app: [Object], L: [Object], Ua: [Object], Sc: null, ca: [Object], td: 1, Qa: [Object], va: [Object], qg: [Object], jc: [Object], ee: [Object], md: [Object], ia: [Object], Xa: [Object], cd: 1, fe: null, K: [Object] }, path: J { o: [Object], Y: 0 }, m: Df { xa: false, ka: false, Ib: false, na: false, Pb: false, oa: 0, kb: '', bc: null, xb: '', Zb: null, vb: '', g: Tc {} }, Kc: false, then: undefined, catch: undefined }, g: Tc {} }
5:23:47.439 PM info sendNewPostNotification
p:0: {"r":4,"a":"n","b":{"p":"/profile-notifications"}}
5:23:47.439 PM info sendNewPostNotification
p:0: Unlisten on /profile-notifications for default
5:23:47.438 PM info sendNewPostNotification
p:0: Unlisten called for /profile-notifications default
5:23:47.437 PM info sendNewPostNotification
event: /profile-notifications:value:{"jane_doe@gmail*com":{"deviceToken2":true},"john_doe@gmail*com":{"deviceToken1":true}}
5:23:47.430 PM info sendNewPostNotification
p:0: handleServerMessage d {"p":"profile-notifications","d":{"jane_doe@gmail*com":{"deviceToken2":true},"john_doe@gmail*com":{"deviceToken1":true}}}
5:23:47.429 PM info sendNewPostNotification
p:0: from server: {"r":2,"b":{"s":"ok","d":{"auth":null,"expires":1491492227}}}
5:23:47.429 PM info sendNewPostNotification
c:0:0: Primary connection is healthy.
5:23:47.290 PM info sendNewPostNotification
p:0: from server: {"r":1,"b":{"s":"ok","d":""}}
5:23:47.289 PM info sendNewPostNotification
p:0: {"r":3,"a":"q","b":{"p":"/profile-notifications","h":""}}
5:23:47.289 PM info sendNewPostNotification
p:0: Listen on /profile-notifications for default
5:23:47.288 PM info sendNewPostNotification
p:0: {"r":2,"a":"gauth","b":{"cred":"ya29.El0lBHwnjCRAbk5q-vLmvHyYlxEv7zKP03kvxA7ktrRQgHpDAhDxM0bkUwfHUNaAJUxK4n7MsS1tTqXBF35NojUCPeMB5CJAhxuI8iTvXEWLvQQW-rbjUwKCIXF9oCQ"}}
5:23:47.287 PM info sendNewPostNotification
p:0: {"r":1,"a":"s","b":{"c":{"sdk.admin_node.4-2-0":1}}}
5:23:47.287 PM info sendNewPostNotification
p:0: reportStats {"c":{"sdk.admin_node.4-2-0":1}}
5:23:47.285 PM info sendNewPostNotification
p:0: connection ready
5:23:47.285 PM info sendNewPostNotification
c:0:0: Realtime connection established.
5:23:47.283 PM info sendNewPostNotification
c:0:0:1 Websocket connected.
5:23:47.262 PM info sendNewPostNotification
c:0:0:1 Websocket connecting to wss://s-usc1c-nss-135.firebaseio.com/.ws?v=5&ns=rally-zone
5:23:47.262 PM info sendNewPostNotification
c:0:0:0 Websocket connection was disconnected.
5:23:47.259 PM info sendNewPostNotification
c:0:0:0 WebSocket is being closed
5:23:47.259 PM info sendNewPostNotification
c:0:0: Shutting down all connections
5:23:47.259 PM info sendNewPostNotification
c:0:0: Reset packet received. New host: s-usc1c-nss-135.firebaseio.com
5:23:47.254 PM info sendNewPostNotification
c:0:0:0 Websocket connected.
5:23:47.167 PM info sendNewPostNotification
c:0:0:0 Websocket connecting to wss://rally-zone.firebaseio.com/.ws?v=5
5:23:47.165 PM info sendNewPostNotification
c:0:0: Connection created
5:23:47.164 PM info sendNewPostNotification
getToken() completed. Creating connection.
5:23:47.162 PM info sendNewPostNotification
p:0: Auth token refreshed
5:23:47.101 PM info sendNewPostNotification
p:0: Making a connection attempt
5:23:47.099 PM info sendNewPostNotification
p:0: Listen called for /profile-notifications default
5:23:47.084 PM info sendNewPostNotification
p:0: Browser went online.
5:23:46.307 PM debug sendNewPostNotification
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
5:23:46.307 PM debug sendNewPostNotification
Function execution started
据我所见,数据库的响应符合我的预期,但是当我尝试记录结果时,我得到了一个很长的 json,与我的预期完全不同。这可能是因为我没有结算帐户吗?有人可以帮我吗?这也是我的代码。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNewPostNotification = functions.database.ref('/posts/{postId}').onWrite(event => {
const postId = event.params.postId;
admin.database.enableLogging(true);
return admin.database().ref("profile-notifications").once('value').then(result => {
if (result) {
console.log('Result: ', result); // "Stuff worked!"
}
}, err => {
console.log('Error: ', err); // Error: "It broke"
});
});
正如 jacobawenger 在评论中指出的那样,问题是 result
是 DataSnapshot
,所以我需要添加 result.val()
来获取它的值。
我尝试创建一个 firebase 函数,用于在将新 post 插入 Firebase 实时数据库时向所有设备发送推送通知。我的设备令牌位于 profile-notifications 端点下。当我尝试通过向数据库插入一个新的 post 来执行我的函数时,我的反应是出乎意料的。我阅读了来自 https://firebase.google.com/docs/functions/ and https://firebase.google.com/docs/reference/admin/node/admin.database 的文档,但无法进一步了解。这是我的日志:
5:23:47.451 PM debug sendNewPostNotification
Function execution took 1145 ms, finished with status: 'ok'
5:23:47.447 PM info sendNewPostNotification
p:0: from server: {"r":4,"b":{"s":"ok","d":""}}
5:23:47.446 PM info sendNewPostNotification
p:0: from server: {"r":3,"b":{"s":"ok","d":{}}}
5:23:47.444 PM info sendNewPostNotification
Result: T { A: P { k: Sb { Ka: [Function: vb], ba: [Object] }, aa: P { k: [Object], aa: null, wb: [Object], Bb: '' }, wb: Zc { ld: [Object], ac: [Object] }, Bb: null }, V: R { u: Gd { app: [Object], L: [Object], Ua: [Object], Sc: null, ca: [Object], td: 1, Qa: [Object], va: [Object], qg: [Object], jc: [Object], ee: [Object], md: [Object], ia: [Object], Xa: [Object], cd: 1, fe: null, K: [Object] }, path: J { o: [Object], Y: 0 }, m: Df { xa: false, ka: false, Ib: false, na: false, Pb: false, oa: 0, kb: '', bc: null, xb: '', Zb: null, vb: '', g: Tc {} }, Kc: false, then: undefined, catch: undefined }, g: Tc {} }
5:23:47.439 PM info sendNewPostNotification
p:0: {"r":4,"a":"n","b":{"p":"/profile-notifications"}}
5:23:47.439 PM info sendNewPostNotification
p:0: Unlisten on /profile-notifications for default
5:23:47.438 PM info sendNewPostNotification
p:0: Unlisten called for /profile-notifications default
5:23:47.437 PM info sendNewPostNotification
event: /profile-notifications:value:{"jane_doe@gmail*com":{"deviceToken2":true},"john_doe@gmail*com":{"deviceToken1":true}}
5:23:47.430 PM info sendNewPostNotification
p:0: handleServerMessage d {"p":"profile-notifications","d":{"jane_doe@gmail*com":{"deviceToken2":true},"john_doe@gmail*com":{"deviceToken1":true}}}
5:23:47.429 PM info sendNewPostNotification
p:0: from server: {"r":2,"b":{"s":"ok","d":{"auth":null,"expires":1491492227}}}
5:23:47.429 PM info sendNewPostNotification
c:0:0: Primary connection is healthy.
5:23:47.290 PM info sendNewPostNotification
p:0: from server: {"r":1,"b":{"s":"ok","d":""}}
5:23:47.289 PM info sendNewPostNotification
p:0: {"r":3,"a":"q","b":{"p":"/profile-notifications","h":""}}
5:23:47.289 PM info sendNewPostNotification
p:0: Listen on /profile-notifications for default
5:23:47.288 PM info sendNewPostNotification
p:0: {"r":2,"a":"gauth","b":{"cred":"ya29.El0lBHwnjCRAbk5q-vLmvHyYlxEv7zKP03kvxA7ktrRQgHpDAhDxM0bkUwfHUNaAJUxK4n7MsS1tTqXBF35NojUCPeMB5CJAhxuI8iTvXEWLvQQW-rbjUwKCIXF9oCQ"}}
5:23:47.287 PM info sendNewPostNotification
p:0: {"r":1,"a":"s","b":{"c":{"sdk.admin_node.4-2-0":1}}}
5:23:47.287 PM info sendNewPostNotification
p:0: reportStats {"c":{"sdk.admin_node.4-2-0":1}}
5:23:47.285 PM info sendNewPostNotification
p:0: connection ready
5:23:47.285 PM info sendNewPostNotification
c:0:0: Realtime connection established.
5:23:47.283 PM info sendNewPostNotification
c:0:0:1 Websocket connected.
5:23:47.262 PM info sendNewPostNotification
c:0:0:1 Websocket connecting to wss://s-usc1c-nss-135.firebaseio.com/.ws?v=5&ns=rally-zone
5:23:47.262 PM info sendNewPostNotification
c:0:0:0 Websocket connection was disconnected.
5:23:47.259 PM info sendNewPostNotification
c:0:0:0 WebSocket is being closed
5:23:47.259 PM info sendNewPostNotification
c:0:0: Shutting down all connections
5:23:47.259 PM info sendNewPostNotification
c:0:0: Reset packet received. New host: s-usc1c-nss-135.firebaseio.com
5:23:47.254 PM info sendNewPostNotification
c:0:0:0 Websocket connected.
5:23:47.167 PM info sendNewPostNotification
c:0:0:0 Websocket connecting to wss://rally-zone.firebaseio.com/.ws?v=5
5:23:47.165 PM info sendNewPostNotification
c:0:0: Connection created
5:23:47.164 PM info sendNewPostNotification
getToken() completed. Creating connection.
5:23:47.162 PM info sendNewPostNotification
p:0: Auth token refreshed
5:23:47.101 PM info sendNewPostNotification
p:0: Making a connection attempt
5:23:47.099 PM info sendNewPostNotification
p:0: Listen called for /profile-notifications default
5:23:47.084 PM info sendNewPostNotification
p:0: Browser went online.
5:23:46.307 PM debug sendNewPostNotification
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
5:23:46.307 PM debug sendNewPostNotification
Function execution started
据我所见,数据库的响应符合我的预期,但是当我尝试记录结果时,我得到了一个很长的 json,与我的预期完全不同。这可能是因为我没有结算帐户吗?有人可以帮我吗?这也是我的代码。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNewPostNotification = functions.database.ref('/posts/{postId}').onWrite(event => {
const postId = event.params.postId;
admin.database.enableLogging(true);
return admin.database().ref("profile-notifications").once('value').then(result => {
if (result) {
console.log('Result: ', result); // "Stuff worked!"
}
}, err => {
console.log('Error: ', err); // Error: "It broke"
});
});
正如 jacobawenger 在评论中指出的那样,问题是 result
是 DataSnapshot
,所以我需要添加 result.val()
来获取它的值。