0
我試圖創建一個firebase函數,用於在將新帖子插入到Firebase實時數據庫時向所有設備發送推送通知。我的設備令牌位於配置文件通知端點下。當我嘗試通過向數據庫插入新帖子來執行我的功能時,我的迴應是意想不到的。我閱讀https://firebase.google.com/docs/functions/和https://firebase.google.com/docs/reference/admin/node/admin.database的文檔,但無法進一步瞭解。這裏是我的日誌:Firebase數據庫查詢返回所有端點的相同數據
5:23:47.451 PM \t debug sendNewPostNotification
Function execution took 1145 ms, finished with status: 'ok'
\t
5:23:47.447 PM info sendNewPostNotification
p:0: from server: {"r":4,"b":{"s":"ok","d":""}}
\t
5:23:47.446 PM info sendNewPostNotification
p:0: from server: {"r":3,"b":{"s":"ok","d":{}}}
\t
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 {} }
\t
5:23:47.439 PM info sendNewPostNotification
p:0: {"r":4,"a":"n","b":{"p":"/profile-notifications"}}
\t
5:23:47.439 PM info sendNewPostNotification
p:0: Unlisten on /profile-notifications for default
\t
5:23:47.438 PM info sendNewPostNotification
p:0: Unlisten called for /profile-notifications default
\t
5:23:47.437 PM info sendNewPostNotification
event: /profile-notifications:value:{"[email protected]*com":{"deviceToken2":true},"[email protected]*com":{"deviceToken1":true}}
\t
5:23:47.430 PM info sendNewPostNotification
p:0: handleServerMessage d {"p":"profile-notifications","d":{"[email protected]*com":{"deviceToken2":true},"[email protected]*com":{"deviceToken1":true}}}
\t
5:23:47.429 PM info sendNewPostNotification
p:0: from server: {"r":2,"b":{"s":"ok","d":{"auth":null,"expires":1491492227}}}
\t
5:23:47.429 PM info sendNewPostNotification
c:0:0: Primary connection is healthy.
\t
5:23:47.290 PM info sendNewPostNotification
p:0: from server: {"r":1,"b":{"s":"ok","d":""}}
\t
5:23:47.289 PM info sendNewPostNotification
p:0: {"r":3,"a":"q","b":{"p":"/profile-notifications","h":""}}
\t
5:23:47.289 PM info sendNewPostNotification
p:0: Listen on /profile-notifications for default
\t
5:23:47.288 PM info sendNewPostNotification
p:0: {"r":2,"a":"gauth","b":{"cred":"ya29.El0lBHwnjCRAbk5q-vLmvHyYlxEv7zKP03kvxA7ktrRQgHpDAhDxM0bkUwfHUNaAJUxK4n7MsS1tTqXBF35NojUCPeMB5CJAhxuI8iTvXEWLvQQW-rbjUwKCIXF9oCQ"}}
\t
5:23:47.287 PM info sendNewPostNotification
p:0: {"r":1,"a":"s","b":{"c":{"sdk.admin_node.4-2-0":1}}}
\t
5:23:47.287 PM info sendNewPostNotification
p:0: reportStats {"c":{"sdk.admin_node.4-2-0":1}}
\t
5:23:47.285 PM info sendNewPostNotification
p:0: connection ready
\t
5:23:47.285 PM info sendNewPostNotification
c:0:0: Realtime connection established.
\t
5:23:47.283 PM info sendNewPostNotification
c:0:0:1 Websocket connected.
\t
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
\t
5:23:47.262 PM info sendNewPostNotification
c:0:0:0 Websocket connection was disconnected.
\t
5:23:47.259 PM info sendNewPostNotification
c:0:0:0 WebSocket is being closed
\t
5:23:47.259 PM info sendNewPostNotification
c:0:0: Shutting down all connections
\t
5:23:47.259 PM info sendNewPostNotification
c:0:0: Reset packet received. New host: s-usc1c-nss-135.firebaseio.com
\t
5:23:47.254 PM info sendNewPostNotification
c:0:0:0 Websocket connected.
\t
5:23:47.167 PM info sendNewPostNotification
c:0:0:0 Websocket connecting to wss://rally-zone.firebaseio.com/.ws?v=5
\t
5:23:47.165 PM info sendNewPostNotification
c:0:0: Connection created
\t
5:23:47.164 PM info sendNewPostNotification
getToken() completed. Creating connection.
\t
5:23:47.162 PM info sendNewPostNotification
p:0: Auth token refreshed
\t
5:23:47.101 PM info sendNewPostNotification
p:0: Making a connection attempt
\t
5:23:47.099 PM info sendNewPostNotification
p:0: Listen called for /profile-notifications default
\t
5:23:47.084 PM info sendNewPostNotification
p:0: Browser went online.
\t
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
\t
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"
});
});
'results'是一個['DataSnapshot'](https://firebase.google.com/docs/reference/admin/node/admin.database.DataSnapshot)對象和日誌記錄,它將會被一系列縮小碼。嘗試記錄['result.val()'](https://firebase.google.com/docs/reference/admin/node/admin.database.DataSnapshot#val)來獲得您想要的實際值。 – jwngr
謝謝,這幫了我一把! –