最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何使用Firebase自动使用新数据更新viewpager?

运维笔记admin13浏览0评论

如何使用Firebase自动使用新数据更新viewpager?

如何使用Firebase自动使用新数据更新viewpager?

我知道Firebase会实时提供数据,但我无法使用新数据更新我的视图而无需刷新整个页面。如何使用新数据更新我的html页面而无需刷新页面。

下面是我的.js文件,我在那里查询Firebase以提取对象,并使用传入的'experience'数组渲染我的'users'html页面,我通过循环遍历列表并填充值来处理我的html。

我希望能够在html中填充新值,因为数据已添加到我的firebase数据库中,我认为'.on(...)'会这样做,但没有运气。

app.get('/users', function(req,res) {
  var experiences = [];
  var ref = firebase.database().ref('my_reference_path');
  ref.on("value", function(snapshot) {
    //promise array to hold all the promises that will be returned from our query
    var promises = [];
    //push each promise to the array to wait to resolve
    snapshot.forEach((snap) => {
      promises.push(firebase.database().ref('ref_path'));
    });
    //once all promises have resolved
    Promise.all(promises).then((snapshots) => {
      //push the resolved objects to the experiences array
      snapshots.forEach((usersnap) => {
          experiences.push(usersnap); //push objects into the "experiences" list
      });
      return experiences
    }).then(experiences => res.render('users', 
      {list: experiences})) //render the view and pass the "experiences" list to the client
  });
});
<div>
  <% for(var i=0; i < list.length; i++) { %>
    <!-- Populate divs with user information -->
    <div> ... </div>
    <div> ... </div>
  <% } %>
</div>
回答如下:

在你的.then设置到你的数据库后,你可以location.reload(),

虽然这很hacky。

使用firebase的现代框架非常棒,因为它们会重新呈现状态更改。我喜欢反应和火力。

或者你可以使用firebase事件监听器,但是,我不确定如何使用vanilla javascript实现。 https://firebase.google/docs/database/web/read-and-write#listen_for_value_events

发布评论

评论列表(0)

  1. 暂无评论