FIrebase jQuery .on方法正在一次而不是一次全部更新数组值

时间:2018-07-26 19:26:41

标签: javascript html firebase firebase-realtime-database

我正尝试更新用户在“公司”页面上下达的订单值,而无需刷新。为此,我使用了jQuery .on方法。但是,这将返回我为订单生成的数组中的值,而不是一次全部生成。这仅仅是firebase的问题,还是我的代码。

这是我的代码:

当我得到值时:

 firebase.database().ref('Orders/' + user_id).on('value',  function(snapshot) {
     // Check if the user has any pending orders 
     if (snapshot.val() === null) {
         // No Pending Orders are Present
          $('.order_content-parent').html(' <div class="order_content">Hooray! You have no pending orders!</div>');

     } else {

         // One or more pending orders are present


          console.log(snapshot.val());


      snapshot.forEach(function(child){
           $('.order_content-parent').html(' <div class="order_content"></div>');

          var order = child.val().Order;

var key = child.key;
  console.log('Key is : '+key);
  getOrders(key);

});

当我将值插入数据库时​​:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
     var myId = user.uid;
     const orders = ['Orders: '];
     $('.postData').each(function() {
    var data = $(this).html();
    orders.push(data);
    var database = firebase.database();
       database.ref('Orders/' + user_id + '/' + myId).set({
    Order: orders
  }, function(error) {
    if (error) {
      // The write failed...
      alert(error);
    } else {
         $('.postData').html('Connecting...');

    }
  });
     database.ref('Orders/' + myId).set({
    Order: orders,
    For: user_id
  }, function(error) {
    if (error) {
      // The write failed...
      alert(error);
    } else {
         $('.postData').html('Order Successfully Placed!');

    }
  });
  });
  } else {
    // No user is signed in.
  }
});

当我从数据库中打印值时,这是我的控制台:

This is my console. As you can see my code is running twice because the machine thinks it was updated twice, but it should only update once because I am inserting the data all at once

这是我的数据库结构:

This is my database structure, as you can see all the data is entered correctly at the same time

任何人都可以帮助

谢谢,

汤姆

1 个答案:

答案 0 :(得分:1)

我认为这是预期的行为,如documentation所述:

  

每次在指定的数据库引用处更改数据(包括对子项的更改)时,都会调用value事件。

由于您的插入内容处于each循环中,因此它们被一个接一个地插入,从而触发.on()侦听器多次。

您可以尝试一次插入所有订单。请尝试这种方法,让我知道它是否有效:

firebase.auth().onAuthStateChanged(function (user) {
    if (!user) {
        console.log("No user is signed in");
        return;
    }
    var myId = user.uid;
    var orders = [];

    // Get the orders to insert first
    $('.postData').each(function () {
        var data = $(this).html();
        orders.push(data);
    });

    // Then, insert them all at once
    var database = firebase.database();
    database.ref('Orders/' + user_id + '/' + myId).set({
        Order: orders
    }, function (error) {
        if (error) {
            // The write failed...
            alert(error);
            return;
        } 
        $('.postData').html('Connecting...');
        database.ref('Orders/' + myId).set({
            Order: orders,
            For: user_id
        }, function (error) {
            if (error) {
                // The write failed...
                alert(error);
                return;
            } 
            $('.postData').html('Order Successfully Placed!');
        });
    });
});