React Native Listview Firebase Database获取对象并显示问题

时间:2018-10-23 02:04:28

标签: reactjs react-native

下面的代码是我从Github复制来学习的代码。

    export default class App extends Component {
        constructor(props){
            super(props)
            const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
            this.state = {
                isLoaded: false,
                isOpenMenu: false,
                dataSource: ds.cloneWithRows([]),
                rotateY: new Animated.Value(0),
                translateX: new Animated.Value(width),
                menuAnimation: new Animated.Value(0),
                text: '',
                uid: '',
                avatarUrl: '',
                userName: '',
                name: '',
                url: ''
            }
        }

和componentWillMount如下所示,并且运行良好:

    async componentWillMount() {
        try {
            let user = await firebase.auth().currentUser
            this.getImageUrl(user.uid, (imageUrl) => {
                this.setState({
                    avatarUrl: imageUrl
                })
            })
            Helpers.getName(user.uid, (name) => {
                this.setState({
                    userName: name
                })
            })
            this.getUserRecipes(user.uid, (recipes) => {
                if(recipes){
                    this.setState({
                        name: recipes.name,
                        url: recipes.url,
                        recipes: recipes.recipes,
                        dataSource: this.state.dataSource.cloneWithRows(recipes.recipes)
                    })
                }
            })
            this.setState({
                uid: user.uid
            })
        } catch(error){
            console.log(error)
        }
    }
    getUserRecipes(userId, callback){
        let userNamePath = "/user/"+userId
        firebase.database().ref(userNamePath).on('value', (snapshot) => {
            let data = snapshot.val()
            if(snapshot){
                if(data.recipes){
                    let obj = {
                        name: data.details.name,
                        url: data.details.url,
                        recipes: data.recipes
                    }
                    callback(obj)
                }
            }
        })

数据库的json文件如下:

    {
      "user" : {
        "0SoP6VeIL3OYuIJuoO6ormnZSK53" : {
          "details" : {
            "bio" : "Male",
            "name" : "MyName",
            "place" : "Korea",
            "url" : "https://firebasestorage.googleapis.com/v0/b/dribblechallengpractice.appspot.com/o/images%2F0SoP6VeIL3OYuIJuoO6ormnZSK53.jpg?alt=media&token=e8e2151f-2940-4a12-a353-a0b11611ac86"
          },
          "recipes" : {
            "1539926978701" : {
              "description" : "No description is dec",
              "food" : "Foodname",
              "image" : "https://firebasestorage.googleapis.com/v0/b/dribblechallengpractice.appspot.com/o/0SoP6VeIL3OYuIJuoO6ormnZSK53%2Fimages%2F1539926955047.jpg?alt=media&token=d55f6f0c-ce25-46af-b745-8997f0a89fa1",
              "title" : "Waterfall"
            },
            "1539927054088" : {
              "description" : "Dadasdsad dsa das",
              "food" : "Fastfood",
              "image" : "https://firebasestorage.googleapis.com/v0/b/dribblechallengpractice.appspot.com/o/0SoP6VeIL3OYuIJuoO6ormnZSK53%2Fimages%2F1539927021648.jpg?alt=media&token=112fa42c-7df6-45bd-895c-4d48821030ed",
              "title" : "Second Title"
            }
          }
        }
      }
    }

现在是问题。 当我使用自己的json数据库时,如下所示: (简化版)

    { 
      "Detail" : {
        "FirstCategory" : {
          "itemname" : "item1",
          "mainimage" : "https://firebasestorage.googleapis.com/v0/b/dribblechallengpractice.appspot.com/o/images%2F0SoP6VeIL3OYuIJuoO6ormnZSK53.jpg?alt=media&token=e8e2151f-2940-4a12-a353-a0b11611ac86",
          "subbrands" : {
            "brandA" : {
              "subbrandsname" : "subname",
              "description" : "No description is desc",
              "image" : "https://firebasestorage.googleapis.com/v0/b/dribblechallengpractice.appspot.com/o/0SoP6VeIL3OYuIJuoO6ormnZSK53%2Fimages%2F1539926955047.jpg?alt=media&token=d55f6f0c-ce25-46af-b745-8997f0a89fa1",
              "title" : "Waterfall",
              "items" : {
                "item1" : {
                  "price" : "500000",
                  "made" : "August, 13, 1985",
                  "itemimage" :"https://firebasestorage.googleapis.com/v0/b/dribblechallengpractice.appspot.com/o/0SoP6VeIL3OYuIJuoO6ormnZSK53%2Fimages%2F1539926955047.jpg?alt=media&token=d55f6f0c-ce25-46af-b745-8997f0a89fa1",
                  "userimage" : {
                    "0SoP6VeIL3OYuIJuoO6ormnZSK53" : {
                      "useritemimage" : "https://firebasestorage.googleapis.com/v0/b/dribblechallengpractice.appspot.com/o/0SoP6VeIL3OYuIJuoO6ormnZSK53%2Fimages%2F1539926955047.jpg?alt=media&token=d55f6f0c-ce25-46af-b745-8997f0a89fa1",
                      "useritemimagetext" : "hello"
                    }
                  }
                },
                "item2" : 
              }
            },
            "brandB" : 
          }
        },
        "SecondCategory" :{
          "itemname" : "item2",
          "mainimage" : "https://firebasestorage.googleapis.com/v0/b/dribblechallengpractice.appspot.com/o/images%2F0SoP6VeIL3OYuIJuoO6ormnZSK53.jpg?alt=media&token=e8e2151f-2940-4a12-a353-a0b11611ac86",
          "subbrands" : 
          }
        },
      }
    }

原始组件的WillMount代码及其修改版本,如下所示:

    async componentWillMount() {
        try {
            let user = await firebase.auth().currentUser
            this.getImageUrl(user.uid, (imageUrl) => {
                this.setState({
                    avatarUrl: imageUrl
                })
            })
            Helpers.getName(user.uid, (name) => {
                this.setState({
                    userName: name
                })
            })
            this.getUserData((item) => {
                if(item){
                    this.setState({
                        name: item.name,
                        subname: item.subname,
                        image: item.image,
                        dataSource: this.state.dataSource.cloneWithRows(item)
                    })
                }
            })
            this.setState({
                uid: user.uid
            })
        } catch(error){
            console.log(error)
        }
    }
    getUserData(callback){
        let userNamePath = "/FirstCategory/"
        firebase.database().ref(userNamePath).on('value', (snapshot) => {
            let data = snapshot.val()
            if(snapshot){
                if(data.item){
                    let obj = {
                        name: data.itemname,
                        subname: data.subbrands.brandA.subbrandsname,
                        image: data.subbrands.brandA.items.item1.itemimage
                    }
                    callback(obj)
                }
            }
        })
    }

该应用程序为空。 如何获得数据库中小写字母的对象而没有错误?

0 个答案:

没有答案