2017-04-06 3 views
0

J'ai essayé de créer une fonction Firebase pour envoyer une notification push à tous les périphériques lorsqu'un nouveau message est inséré dans la base de données Firebase Realtime. Mes jetons d'appareil sont sous le point de terminaison des notifications de profil. Lorsque j'essaie d'exécuter ma fonction en insérant un nouveau message dans la base de données, ma réponse est inattendue. J'ai lu les documents de https://firebase.google.com/docs/functions/ et https://firebase.google.com/docs/reference/admin/node/admin.database, mais je n'ai pas pu obtenir beaucoup plus loin. Voici mes journaux:Requête de base de données Firebase retournant les mêmes données pour tous les points d'extrémité

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

D'après ce que je peux voir, la réponse de la base de données est ce que je pense, mais quand je tente de me connecter mon résultat, je reçois une longue JSON avec rien de proche de ce à quoi je m'attendais. Serait-ce parce que je n'ai pas de compte de facturation? Quelqu'un peut-il m'aider à ce sujet? Voici mon code aussi.

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" 
    }); 
}); 
+0

'results' est un objet [' DataSnapshot'] (https://firebase.google.com/docs/reference/admin/node/admin.database.DataSnapshot) et une connexion qui va être un tas de minified code. Essayez de vous connecter ['result.val()'] (https://firebase.google.com/docs/reference/admin/node/admin.database.DataSnapshot#val) à la place pour obtenir la valeur réelle souhaitée. – jwngr

+0

Merci, cela m'a aidé! –

Répondre

0

Comme jacobawenger a souligné dans le commentaire, le problème était que le result était un DataSnapshot, donc je besoin d'ajouter result.val() pour obtenir la valeur de celui-ci.