Converting website over to Laravel, and have a weird error that is driving me mad.
Got a controller, firing out reviews (randomised to page). This should be producing JSON as standard in laravel which it is, however this javascript intermittently fails!
The error is reviews.forEach is not a function
I've looked at the JSON and it's intermittently not providing indexes? Maybe 1/10 it'll produce a working JSON that won't crash the JS code.
Working JSON (from Chrome Console via a console.log)
Failing JSON feed
Cookies and kudos if you can crack the puzzle!
Got a controller, firing out reviews (randomised to page). This should be producing JSON as standard in laravel which it is, however this javascript intermittently fails!
PHP:
<script type="text/javascript">
jQuery.ajax({
url: 'review/random/3',
dataType: 'json',
method: 'GET',
success: function(data){reviews_data(data)}
});
function reviews_data(data)
{
reviews = data;
console.log (reviews);
reviews.forEach (function(review) {
document.getElementById("reviews").innerHTML+= '<div class="col-sm-4 portfolio-item" id="review"> <div class="review" data-stars="1,2,3,4,5"> <p class="reviewContent">' + review.reviewtext + '</p> <p class="reviewName">' + review.name + '</p> <div class="rating-score"> <div class="rating-5">' + review.reviewrating + '</div> </div> </div> </div>';
});
};
</script>
The error is reviews.forEach is not a function
I've looked at the JSON and it's intermittently not providing indexes? Maybe 1/10 it'll produce a working JSON that won't crash the JS code.
Working JSON (from Chrome Console via a console.log)
PHP:
Object {1: Object, 2: Object, 3: Object}
1: Object
created_at: "2015-12-24 15:59:42"
id: 2
name: "Review two"
published: 1
published_at: "0000-00-00 00:00:00"
reviewrating: 3
reviewtext: "This is some text for the review"
updated_at: "2015-12-24 15:59:42"
__proto__: Object
2: Object
created_at: "2015-12-24 15:59:42"
id: 4
name: "Review four"
published: 1
published_at: "0000-00-00 00:00:00"
reviewrating: 3
reviewtext: "This is some text for the review"
updated_at: "2015-12-24 15:59:42"
__proto__: Object
3: Object
created_at: "2015-12-24 15:59:42"
id: 5
name: "Review Five"
published: 1
published_at: "0000-00-00 00:00:00"
reviewrating: 5
reviewtext: "This is some text for the reviewThis is some text for the reviewThis is some text for the reviewThis is some text for the reviewThis is some text for the review"
updated_at: "2015-12-24 15:59:42"
__proto__: Object
__proto__: Object
Failing JSON feed
PHP:
[Object, Object, Object]
0: Object
created_at: "2015-12-24 15:59:42"
id: 1
name: "Review one"
published: 1
published_at: "0000-00-00 00:00:00"
reviewrating: 5
reviewtext: "This is some text for the review"
updated_at: "2015-12-24 15:59:42"
__proto__: Object
1: Object
created_at: "2015-12-24 15:59:42"
id: 2
name: "Review two"
published: 1
published_at: "0000-00-00 00:00:00"
reviewrating: 3
reviewtext: "This is some text for the review"
updated_at: "2015-12-24 15:59:42"
__proto__: Object
2: Object
created_at: "2015-12-24 15:59:42"
id: 4
name: "Review four"
published: 1
published_at: "0000-00-00 00:00:00"
reviewrating: 3
reviewtext: "This is some text for the review"
updated_at: "2015-12-24 15:59:42"
__proto__: Object
length: 3
__proto__: Array[0]
Cookies and kudos if you can crack the puzzle!