【问题标题】:How to traverse through the auto key generated by firebase push() in React Native?如何在 React Native 中遍历 firebase push() 生成的自动密钥?
【发布时间】:2017-06-23 02:17:02
【问题描述】:

我创建了一个搜索来查询专辑的 discogs api。当我刷专辑时,它会被推送到实时数据库中。 Firebase 自动为每个推送的项目生成密钥,这很棒,但我不确定如何处理这些密钥。如何将图像 url 放入我的 require 语句中?我

这是推送到数据库的函数。

saveToCollection() {
    const myFirebaseRef = firebase.database().ref();
    myFirebaseRef.push({
      albums: `${this.props.album.cover}`
    });
}

任何帮助将不胜感激!

用 cmets 的代码更新:

componentWillMount() {
    const rootRef = firebase.database().ref();
    const albumRef = rootRef.child('albums');
    albumRef.once('value', (snapshot) => {
        snapshot.forEach((childSnapshot) => {
            var childKey = childSnapshot.key;
            var childData = childSnapshot.val(); // ... this.setState({albums: childKey.childData}) }); }); }// This is just a sample script. Paste your real code (javascript or HTML) here.

            if ('this_is' == /an_example/) {
                of_beautifier();
            } else {
                var a = b ? (c % d) : e[f];
            }

【问题讨论】:

  • 要从数据库中获取数据,您需要附加一个监听器,然后处理数据快照。例如,请参阅firebase.google.com/docs/database/web/…
  • 这似乎是一个非常不同的错误。根据您发布的内容,我无法判断错误来自何处。

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


【解决方案1】:

您可以使用 for...in 迭代 firebase 自动生成的键并将每个值推入一个数组,然后使用 Mustache {{#.}} ... {{/.}} 标记对进行迭代数组中的每个对象。

let arr = [];

for(let autoKey in childData){
    arr.push(childData[autoKey]);
}

let tpl = '{{#.}} {{albums}} {{/.}}';

Mustache.render(tpl, arr);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 2017-08-22
    • 2018-03-04
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多