0

I am doing the following:

  $.getJSON(url,function(data){
    var totalQueries = data.length;
    $.each(data, function(i, item) {

But this one looks like to be wrong:

var totalQueries = data.length;

As by the end of it I check for the last item and it never happens:

  if (i === totalQueries - 1) {
    myId = item.pageid;
    console.log(myId);
    newQuery();
  }
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • 1
    Are you getting console errors? Have you tried debugging? How confident are you that `data` is an array and not an object? – Alexander Nied Jul 30 '17 at 03:02
  • I am sure `data` is an array as I can read it, I get no errors as the code is correct, but I can see that `console.log(myId);` doesn't happen which means the conditional isn't happening @AlexanderNied – rob.m Jul 30 '17 at 03:03
  • 1
    `total length of a function(data)` makes no sense ... you're getting the length property of the returned parsed JSON. If the top level "object" is an Array, the length property is the number of elements in the array – Jaromanda X Jul 30 '17 at 03:06
  • perhaps an example of `data` would help - perhaps the last item in the array is not an object with a pageid property – Jaromanda X Jul 30 '17 at 03:07
  • @JaromandaX exactly, I knew it wasn't making any sense, that's why I have ended up on here. Is there anyway I could do to know the end of it? – rob.m Jul 30 '17 at 03:07
  • @JaromandaX here it is jsFiddle, check the beginning of function `wikiText() {` where I get the data and the end of that when I run the conditional – rob.m Jul 30 '17 at 03:09
  • @rob.m - I think you accidentally omitted the JSFiddle link from your above comment... – Alexander Nied Jul 30 '17 at 03:12
  • @AlexanderNied ouch https://jsfiddle.net/jikupin/pz3ce0o6/411/ – rob.m Jul 30 '17 at 03:12
  • @JaromandaX sorry, this is the fiddle https://jsfiddle.net/jikupin/pz3ce0o6/411/ – rob.m Jul 30 '17 at 03:12
  • `i` is a string `"parse"` and `totalQueries` is `undefined` at linked jsfiddle `console.log(i, typeof i, totalQueries)`. What are you trying to achieve? Are you trying to perform tasks asynchronously, or in a sequence, then do stuff when all of the tasks have completed? – guest271314 Jul 30 '17 at 03:30
  • @guest271314 I am trying to find the last id of the queries and then start again to query starting from that id, the way i start again is by calling `newQuery();` and passing the latest `id myId = item.pageid;` at the end of the loop to `newQuery();` api parameter `eicontinue=2|"+myId+"` so that i can start again the calls starting from the last id, does it make sense? – rob.m Jul 30 '17 at 03:33
  • You can `return jQuery.getJSON()` jQuery promise from the functions which makes the asynchronous calls. Use `$.when()` and `Function.prototype.apply()` to perform a task when all elements of array return a jQuery promise object. Or you can use jQuery `.queue()` and `.promise()` to achieve similar result. – guest271314 Jul 30 '17 at 03:39
  • @guest271314 could you elaborate it more into something i could test pls? – rob.m Jul 30 '17 at 03:40
  • @guest271314 I just added `.promise().done(function( arg1 ) { myId = item.pageid; console.log(myId); newQuery(); });` at the end of the function but the console says `item` is not defined – rob.m Jul 30 '17 at 03:44
  • doing `.queue().promise(function..` gives me `$.getJSON(...).queue is not a function` – rob.m Jul 30 '17 at 03:47
  • @rob.m Using `$.when()` https://jsfiddle.net/s5f4dc6e/. Note, `.queue()` requires a `queueName` parameter equal to `queueName` passed to `.promise()`. `.queue()` cannot not be chained to `$.getJSON()`, you would use `.queue()` to call N functions in sequence, then `.promise()` chained before `.then()` to schedule further tasks, which could include calling same function which returns `.queue()` with `.promise()` chained again – guest271314 Jul 30 '17 at 03:58
  • @guest271314 oh ok, anyhow you could apply that to the linekd fiddle so that i can better understand it? I am not too familiar with queue and i am not sure where to insert it then – rob.m Jul 30 '17 at 04:00
  • See [Execute function queue in javascript](https://stackoverflow.com/q/32028368/), [javascript, $.ajax, variable name](https://stackoverflow.com/q/38758499/) – guest271314 Jul 30 '17 at 04:04
  • @guest271314 I think I have a much simpler solution, since the calls of `newQuery()` are the same as per `wikitext()`, I made 2 `counters`, one for `newQuery()` and one for `wikitext()`, and I check when they get o each last one, and when they finally match in the number, i start a `newQuery()` https://jsfiddle.net/jikupin/pz3ce0o6/458/ – rob.m Jul 30 '17 at 04:39

1 Answers1

0

data is object use Object.keys(data).length; to count and here you object look like,

i is parse

{
  "parse": {
    "title": "Colegio Nueva Granada",
    "pageid": 2340380,
    "text": {
      "*": "<div class=...."
    },
    "langlinks": []
  }
}
ewwink
  • 18,382
  • 2
  • 44
  • 54
  • I tried assigning `var totalQueries = Object.keys(data).length;` but I never get the `console.log(myId);` – rob.m Jul 30 '17 at 03:39
  • because `i` is object key `parse` not interger, you need to create counter. – ewwink Jul 30 '17 at 03:45
  • can you edit my fiddle so that i better understand your answer please? Because I am not sure to put the counter – rob.m Jul 30 '17 at 03:57
  • here https://jsfiddle.net/ewwink/72uL633e/ edited: #65, $199, added: #66, #197, #198, 204# beware it load huge file, disable your internet before it hang your browser – ewwink Jul 30 '17 at 04:31
  • hey thanks, I am not sure why it is loading so many if the continue parameter is 2 and i can see a lot of duplicate ids tho – rob.m Jul 30 '17 at 04:34
  • I think I have a much simpler solution, since the calls of `newQuery()` are the same as per `wikitext()`, I made 2 `counters`, one for `newQuery()` and one for `wikitext()`, and I check when they get o each last one, and when they finally match in the number, i start a `newQuery()` https://jsfiddle.net/jikupin/pz3ce0o6/458/ – rob.m Jul 30 '17 at 04:39