Firebase函数如何从Firebase数据库获取值数组

时间:2018-10-04 18:58:26

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

我想在第二个函数中获取数据库中所有用户的数组,该函数使用“”返回admin.database()。ref(“ / Usuarios”)。once('value')  ”,以便我们可以向所有这些用户发送通知。所有用户如何才能使用第二种功能?谢谢

我有此代码:

open override func drawValues(context: CGContext)
{
    guard
        let dataProvider = dataProvider,
        let lineData = dataProvider.lineData
        else { return }

    if isDrawingValuesAllowed(dataProvider: dataProvider)
    {
        var dataSets = lineData.dataSets

        let phaseY = animator.phaseY

        var pt = CGPoint()

        for i in 0 ..< dataSets.count
        {
            guard let dataSet = dataSets[i] as? ILineChartDataSet else { continue }


            if !shouldDrawValues(forDataSet: dataSet)
            {
                continue
            }

            let valueFont = dataSet.valueFont

            guard let formatter = dataSet.valueFormatter else { continue }

            let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)
            let valueToPixelMatrix = trans.valueToPixelMatrix

            let iconsOffset = dataSet.iconsOffset

            // make sure the values do not interfear with the circles
            var valOffset = Int(dataSet.circleRadius * 1.75)

            if !dataSet.isDrawCirclesEnabled
            {
                valOffset = valOffset / 2
            }

            _xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator)

            for j in stride(from: _xBounds.min, through: min(_xBounds.min + _xBounds.range, _xBounds.max), by: 1)
            {
                guard let e = dataSet.entryForIndex(j) else { break }

                if(j == 0 || j == dataSet.entryCount - 1)
                {
                    pt.x = CGFloat(e.x)
                    pt.y = CGFloat(e.y * phaseY)
                    pt = pt.applying(valueToPixelMatrix)

                    if (!viewPortHandler.isInBoundsRight(pt.x))
                    {
                        break
                    }

                    if (!viewPortHandler.isInBoundsLeft(pt.x) || !viewPortHandler.isInBoundsY(pt.y))
                    {
                        continue
                    }

                    if dataSet.isDrawValuesEnabled {
                        ChartUtils.drawText(
                            context: context,
                            text: formatter.stringForValue(
                                e.y,
                                entry: e,
                                dataSetIndex: i,
                                viewPortHandler: viewPortHandler),
                            point: CGPoint(
                                x: pt.x,
                                y: pt.y - CGFloat(valOffset) - valueFont.lineHeight),
                            align: .center,
                            attributes: [NSAttributedStringKey.font: valueFont, NSAttributedStringKey.foregroundColor: dataSet.valueTextColorAt(j)])
                    }

                    if let icon = e.icon, dataSet.isDrawIconsEnabled
                    {
                        ChartUtils.drawImage(context: context,
                                             image: icon,
                                             x: pt.x + iconsOffset.x,
                                             y: pt.y + iconsOffset.y,
                                             size: icon.size)
                    }

                }
                else{



                }


            }
        }
    }
}

1 个答案:

答案 0 :(得分: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);
        });