简体   繁体   中英

how to find length of an array in javascript

I am working with firebase for the first time. For that I am using Firestore. For getting all the messages I use Firebase built in method querySnapshot ; Here i am getting all the rows as an object, where I declare a global variable as an array and try to push each and every object into that array. All was perfect. I got the array with all the messages as I want. But I want to get the length of an array. And I dont know every time I got 0 as an output where It have two rows.Lets Look at the Image for more better understanding在此处输入图片说明

Please help me solve this..

在此处输入图片说明 Here is the utput i get

我想在onSnapshot回调user_messages任何内容推送到user_messages数组之前调用showMessages()

onSnapshot lets you listen for snapshots. That means that you cannot guarantee that your handler would have been called even once before showMessages is invoked.

You could reorder your control flow like this:

// ...

user_chats.onSnapshot((querySnapshot) => {
    querySnapshot.forEach(doc => {
        user_messages.push(doc.data());
    });
    showMessages();
});

// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM