【问题标题】:Cloud functions - Save data with loop云函数 - 使用循环保存数据
【发布时间】:2020-04-24 22:42:30
【问题描述】:

我正在尝试通过循环列表在 Firebase 数据库上实时设置一个对象。 例如,我有对象列表。我想遍历该列表,并且每个循环都实时保存在数据库中的对象。 这是代码:

  if(feedVideosList.length > 0){
       feedVideosList.forEach(video =>{
         return admin.database().ref('/userFeed/${userUID}/').set(video);
     }).catch(error =>{
      console.log("Error user feed:  ",error);
    });
  }else{
    console.log("Problem with the videos list:  ",error);
  }

我很确定这段代码是错误的,我不应该这样写。 就像我说的那样,我要做的是遍历一个列表并每次将数据保存在 spasific 节点上。 问题是我应该怎么做,正确的方法是什么?

【问题讨论】:

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


    【解决方案1】:

    如果您在 Cloud Functions 中运行此代码,我最好的猜测是您希望 Cloud Function 在完成对数据库的所有写入后终止。您可以为此使用Promise.all()

      if(feedVideosList.length > 0){
        let promises = []
         feedVideosList.forEach(video =>{
           promises.push(admin.database().ref('/userFeed/${userUID}/').set(video));
         })
         return Promise.all(promises).catch(error =>{
          console.log("Error user feed:  ",error);
        });
      }else{
        console.log("Problem with the videos list:  ",error);
      }
    

    【讨论】:

    • 正是我需要的
    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多