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

javascript - forEach is not a function when trying to loop through Firebase result set: - Stack Overflow

programmeradmin4浏览0评论

I am trying to port some of my Firebase database calls to an IOT board that does not have jQuery, just good old JavaScript.

In my code I originally had a jQuery $.each(tripData, function(index, element) ... loop to iterate through my results.

I have switched this to:

var tripsRef;
tripsRef = firebase.database().ref('trips/');
tripsRef.orderByChild('timestamp').limitToLast(100).on('value', function (response) {
var tripData = response.val();
tripData.forEach(function (index, element) {
    if (element.status >= 0) {
        var trip = new Object();
        trip.id = index;
        trip.launch = element.launch;
        trip.status = element.status;
    }
});

... but, I am getting the following error:

forEach is not a function

I am not sure how to resolve this.

I am trying to port some of my Firebase database calls to an IOT board that does not have jQuery, just good old JavaScript.

In my code I originally had a jQuery $.each(tripData, function(index, element) ... loop to iterate through my results.

I have switched this to:

var tripsRef;
tripsRef = firebase.database().ref('trips/');
tripsRef.orderByChild('timestamp').limitToLast(100).on('value', function (response) {
var tripData = response.val();
tripData.forEach(function (index, element) {
    if (element.status >= 0) {
        var trip = new Object();
        trip.id = index;
        trip.launch = element.launch;
        trip.status = element.status;
    }
});

... but, I am getting the following error:

forEach is not a function

I am not sure how to resolve this.

Share Improve this question asked Apr 7, 2017 at 22:25 eat-sleep-codeeat-sleep-code 4,85516 gold badges55 silver badges101 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 9
for(let index in tripData){
  element = trimpData[index];
}

not realy foreach, but works exactly like it

but you also can use map functions

You should really figure out if your response is an Array or Object. $.each() iterates over arrays AND objects, thats why it works.

you should use for...in statement if you really want to iterate over this object tripData.

for(let prop in tripData)
{ 
   if (tripData.hasOwnProperty(index))
   { 
     item = tripData[prop];
      // do stuff
    }
}

lear about for...in statement here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for...in

While the answer by @TypedSource will iterate over the resulting children, the order in which it iterates is undetermined - and very likely not going to be by timestamp.

A snapshot that you get as the result for a query contains three pieces of information for each child: its key, its value, and its position relative to the other children. When you call .val() on the snapshot, you lose the relative ordering.

To maintain the order, use the built-in forEach() method of the snapshot:

var tripsRef;
tripsRef = firebase.database().ref('trips/');
tripsRef.orderByChild('timestamp').limitToLast(100).on('value', function (response) {
  var index = 0;
  response.forEach(function (child) {
    var element = child.val();
    if (element.status >= 0) {
      var trip = new Object();
      trip.id = index;
      trip.launch = element.launch;
      trip.status = element.status;
    }
    index++;
  });
});

Array.of(response.val()).forEach should work if it is just an array-like object missing its iterator

发布评论

评论列表(0)

  1. 暂无评论