如何从Firebase数据快照中检索数据

时间:2019-04-10 20:18:27

标签: javascript android react-native firebase-realtime-database

我正在使用firebase开发一个react native应用。当我从Firebase获取数据时,我得到了这样的数据快照

  

{“ 674cafe2-e886-4d9d-b547-b5405d902072”:{“ description”:“ 111”,“ imageUrls”:{“ 0”:“ https://firebasestorage.googleapis.com/v0/b/aam-623b6.appspot.com/o/flyers%2F674cafe2-e886-4d9d-b547-b5405d902072%2F2.jpg?alt=media&token=c56cc053-7167-44eb-9f8b-fb55664eacac”,“ 1”:“ {{3 }}}},“ latitutde”:25.197669830084113,“经度”:46.33913576602936,“商店名称”:“ 3023b667-3cb2-400e-8a36-1113be9e816c”,“标题”:“ 111”,“用户”:“ 8Igfk0bT06MOkzrP8Y5njOdfi}

使用snap.child.val()我能够从描述,经度,纬度等中检索数据,但无法检索imageUrls ...因为它包含child或其数组。如何从imageUrls中检索第一个孩子

https://firebasestorage.googleapis.com/v0/b/aam-623b6.appspot.com/o/flyers%2F674cafe2-e886-4d9d-b547-b5405d902072%2F1.jpg?alt=media&token=f1693b41-28db-4129-a5c7-190b96656e5b

1 个答案:

答案 0 :(得分:2)

以下是抓取imageUrls的代码:

  firebase.database().ref(YOUR_PATH).on('value', snap => {
        if (snap.exists()) {    
            let stringifyObject = JSON.stringify(snap)
            let obj = JSON.parse(stringifyObject);
            obj.key = snap.key
            console.log(JSON.stringify(obj.imageUrls)) // This will print your array and you do work around it
        })
相关问题