【问题标题】:How to store list of objects for each user in firebase如何在firebase中为每个用户存储对象列表
【发布时间】:2018-05-04 17:53:51
【问题描述】:

我正在尝试使用 firebase 编写应用程序。我想将 JSON 搜索对象存储在 searches/ 中,并将对它们中的每一个的引用存储在属于进行搜索的用户的表中。这是我的尝试:

  var firebase = require("firebase");
  firebase.initializeApp(firebaseConfig);
  var database = firebase.database();

  /*
  * Inserts a search into the database
  */
  this.addSearchToDB = function(positive, negative, neutral){
    let today = new Date();
    let dateCreated = today.getFullYear()+"-"+(today.getMonth()+1)+"-"+today.getDate();
    var search = {
                "query": searchInput,
                "location": location,
                "until": date,
                "dateCreated": dateCreated,
                "amount": tweetAmount,
                "positive": positive,
                "negative": negative,
                "neutral": neutral
                };
    //setup of path to reference the data
    var searchesRef = database.ref("searches");
    var newSearchKey = searchesRef.push(search).key;

    console.log("newSearchRef key:");
    console.log(newSearchKey);

    let user = firebase.auth().currentUser;
    let uid = user.uid;

    console.log("Curr user id: "+uid);

    let userRef = database.ref("users/"+uid);
    let currUserSearches;
    userRef.once("value").then( (value) => {
      currUserSearches = value;
    });

    console.log("Current user searches");
    console.log(currUserSearches);

    if (currUserSearches === undefined)
      currUserSearches = [];

    currUserSearches.push(newSearchKey);

    userRef.set(currUserSearches).then( () => {
      database.ref("users/"+uid).once("value").then((value)=>{
        console.log(value.val());
      });
    });
  }

在第一次插入时,会发生这种情况:

  1. 我收到了newSearchKey(成功登录到控制台)
  2. 我得到了currentUser 的用户ID(成功登录到控制台)
  3. currUserSearches 未定义。 (将undefined 记录到控制台)
  4. 在userRef.set() 回调中,找到一个包含newSearchKey 的列表并将其打印到控制台。

这一切都很好。这是我对第一个插入的期望。但是,当我再次插入时,完全相同的过程会重复,这意味着 currUserSearches 再次未定义。这当然是错误的。 currUserSearches 应该包含我刚刚插入的那个密钥。但是好像忘记了我插入了什么。

这里发生了什么,我怎样才能实现我想要的行为?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

    这是因为对 Firebase 数据库的所有查询(读取和写入)都是异步的。 console.log #3 在userRef.once("value") 返回结果之前执行。

    你应该把promise链起来,如下:

    let userRef = database.ref("users/"+uid);
    let currUserSearches;
    
    userRef.once("value")
    .then( (value) => {
      currUserSearches = value;
    
      if (currUserSearches === undefined)
            currUserSearches = [];
    
          currUserSearches.push(newSearchKey);
    
          return userRef.set(currUserSearches);
    })
    .then( () => {
      return database.ref("users/"+uid).once("value");  // <- Actually could be userRef.once("value") 
    
    })
    .then((value)=>{
        console.log(value.val());
    });
    

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 2020-10-06
      • 1970-01-01
      相关资源
      最近更新 更多