C.apply is not a function

This code gives me this error: c.apply is not a function All code works well only if I use one function. However, I am not sure how to use the two functions. These lines are probably erroneous:

  postHandler(<?php echo get_posts($db, 0, $_SESSION['posts_start']); ?>, <?php echo get_posts1($db, 0, $_SESSION['posts_start']); ?>); 

and

 var postHandler = function(postsJSON, postsJSON1) { $.each(postsJSON, postsJSON1, function(i, post, post1) { 

script

first function

 function get_posts($db, $start, $number_of_posts) { //code return json_encode($posts); } 

exit:

 string '[{"username":"Altitude software","foto_oferta":"thumb\/miniaturas\/oferta\/default_offer.jpg","actividades":"Some activities","id_oferta":77,"oferta":"Programador web" ... 

second function

 function get_posts1($db, $start, $number_of_posts) { //code return json_encode($posts1); } 

exit:

 string '[{"id_offer":77,"tags":["c++","JAVA"]},{"id_offer":76,"tags":["ajax","php"]},{"id_offer":75,"tags":["PHP","JAVA"]}]' 

Js

 var postHandler = function(postsJSON, postsJSON1) { $.each(postsJSON, postsJSON1, function(i, post, post1) { var id = 'post-' + post.id_oferta; $('<div></div>').addClass('post').attr('id',id) .html('<div class="box_offers"><div class="rating_offer"></div><div class="post-title">' + post.oferta + '</div> <div class="post-box"> <a class="oferta_val bold_username">' + post1.tags + '</a></div><a id='+id+'hide class="post-more" >DescriΓ§Γ£o</a><div class="logo_offer">') .appendTo($('#posts')); $('#'+id+'hide').click(function() { $('.'+id+'hidden').slideToggle('fast'); }); }); }; postHandler(<?php echo get_posts($db, 0, $_SESSION['posts_start']); ?>, <?php echo get_posts1($db, 0, $_SESSION['posts_start']); ?>); 
+6
source share
1 answer

I believe the problem is this line:

 $.each(postsJSON, postsJSON1, function(i, post, post1) { 

The generic iterator $.each() function accepts only two parameters, the second of which should be a function. Similarly, the callback function you provide must take two parameters.

What is your intention if you order two objects at the same time? If you can describe your data structures and explain what you want to do, I could make some suggestions. (Show your JSON ...)

UPDATE: OK, based on updating the question, both postsJSON and postsJSON1 are arrays. Considering how you tried to use both from within the same function, it seems that the elements in the arrays have a one-to-one relationship, that is, postsJSON[0] refers to postsJSON1[0] , postsJSON[1] refers to postsJSON1[1] postsJSON[2] refers to postsJSON1[2] , etc. etc.

If so, the smallest possible change to your existing code to make it work would be as follows:

 var postHandler = function(postsJSON, postsJSON1) { $.each(postsJSON, function(i, post) { var post1 = postsJSON1[i]; // the rest of your code from inside your $.each() here }); }; 

That is, continue to use $.each() , but noting that it can only iterate over one array at a time, using the provided index i to iterate over another array in parallel, setting it manually as the first line of the function.

Or perhaps a more obvious way to do this with the traditional for loop:

 var postHandler = function(postsJSON, postsJSON1) { var i, post, post1; for (i = 0; i < postsJSON.length; i++) { post = postsJSON[i]; post1 = postsJSON1[i]; // the rest of your code from inside your $.each() here } }; 

In any case, both arrays are processed in parallel. Obviously, this assumes that the arrays are the same length and that the elements of any index number will be the elements that you want to associate with each other, as mentioned above - if not, then you will need to provide more detailed information about how the arrays are connected.

+6
source

Source: https://habr.com/ru/post/905485/


All Articles