【问题标题】:Firebase functions how to get an array of values from Firebase DatabaseFirebase 函数如何从 Firebase 数据库中获取一组值
【发布时间】:2019-03-10 05:47:30
【问题描述】:

我想在我使用的第二个函数中获取我的数据库的所有用户的数组 ""return admin.database().ref("/Usuarios").once('value') " 这样我们就可以向所有这些用户发送通知。所有用户如何才能进入第二个功能?谢谢

我有这个代码:

let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotificationNewAd = functions.database.ref('/Alertas/{notiId}').onWrite((change, context) => {

    // Only edit data when it is first created.
    if (change.before.exists()) {
    return null;
    }
    // Exit when the 

data is deleted.
        if (!change.after.exists()) {
        return null;
        }


    //te escribe el json de el mensaje nuevo
    const afterData = change.after.val();
    console.log("afterData: ", afterData);

    //get lat and lng of Ad
    const name = afterData.name;
    console.log("Name: "+name);

    //get lat and lng of Ad
    const lat = afterData.lat;
    const lng = afterData.lng;
    console.log("Lat y Lng", "lat: "+lat+" lng: "+lng);

    //get lat and lng of Ad
    const adType = afterData.typeAd;
    console.log("Tipo: "+adType);

    //get the user id of the ad
    const notiId = context.params.notiId;
    console.log("notiId: ", notiId);

    const userId = afterData.userId;
    console.log("userId: ", userId);

   return admin.database().ref("/Usuarios").once('value')
    .then(snap => {
        const userName = snap.child("name").val();
        return console.log("userName: ", userName);
    });
 });

【问题讨论】:

  • 如果您使用onCreate 而不是onWrite,那么您不需要检查之前/之后是否存在。 onCreate 的第一个参数是 snap 而不是变化增量,例如onCreate( (snap, context) => {})Docs

标签: node.js firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions


【解决方案1】:

看起来这就是你要问的:

exports.sendNotificationNewAd =
    functions.database.ref('/Alertas/{notiId}')
        .onCreate((noti_snap, context) => {

            //te escribe el json de el mensaje nuevo
            const notif = noti_snap.val();
            console.log("notif: ", notif);

            //get lat and lng of Ad
            const name = notif.name;
            console.log("Name: " + name);

            //get lat and lng of Ad
            const lat = notif.lat;
            const lng = notif.lng;
            console.log("Lat y Lng", "lat: " + lat + " lng: " + lng);

            //get lat and lng of Ad
            const adType = notif.typeAd;
            console.log("Tipo: " + adType);

            //get the user id of the ad
            const notiId = context.params.notiId;
            console.log("notiId: ", notiId);

            const userId = notif.userId;
            console.log("userId: ", userId);

            return admin.database().ref("/Usuarios").once('value')
                .then(snap => {
                    let children = [];
                    snap.forEach(child_snap => {
                        children.push(child_snap.val()); // build children
                    });

                    return children;
                })
                .then(children => {
                    children.map(child => {
                        let message = {
                            notification: {
                                title: "message",
                                body: "body"
                            },
                            token: child.device_token
                        }

                        admin.messaging().send(message).catch(console.log);
                    });

                    return null;
                })
                .then( () => { 
                    return notif.ref.remove(); // consume send request
                })
                .catch(console.log);
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多