0

This is my code:

jira.searchJira('priority = High', null, function(error, issuesArray) {
  if (error) {
    next(new Error(error));
  } 
  issuesObject = issuesArray[Object.keys(issuesArray)[4]];                  //All the issues
  var singleIssue = issuesObject[issuesObject[Object.keys(issuesObject)[0]]]; //gets issue at position 0 in object

  console.log(singleIssue);
  console.log(typeof(singleIssue));
});

I am searching through JIRA using a query string (priority = High) to search for a list of issues in that filter. The documentation I have been following says that the callback should return an array of issues but, as you can see, I was able to do Object.keys()[] to return the issues (even though Array.isArray(issueArray) returned true).

What I am trying to achieve is to loop through my Array/Object of issues (issuesObject) to retrieve each individual issue. singleIssue returns the issue at position 0 but I want a way to loop through to find ALL the issues. I have tried for...in issuesObject but it just counts up listing the index (i.e. from 0 to number of issues).

Nuwan
  • 1,226
  • 1
  • 14
  • 38
wmash
  • 4,032
  • 3
  • 31
  • 69
  • can you provide your link of document you said – yelliver Sep 22 '15 at 09:51
  • https://raw.githubusercontent.com/steves/node-jira/master/lib/jira.js - search for `searchJira` on the document to find the breakdown of the function – wmash Sep 22 '15 at 10:06

1 Answers1

3

There is a response example here (you'll have to click "expand"):

{
    "expand": "names,schema",
    "startAt": 0,
    "maxResults": 50,
    "total": 1,
    "issues": [
        {
            "expand": "",
            "id": "10001",
            "self": "http://www.example.com/jira/rest/api/2/issue/10001",
            "key": "HSP-1"
        }
    ]
}

So, you should be able to do the following:

jira.searchJira('priority = High', null, function(error, searchResults) {
   // Error handling ...
   var allIssues = searchResults.issues; // <-- this is an array.
   var singleIssue = allIssues[0]; // 0 or any other index. 
   // singleIssue is an object as well.

   // print all id's of issues:
   allIssues.forEach(function(issue) {
      console.log(issue.id); 
   });
});
Artyom Neustroev
  • 8,627
  • 5
  • 33
  • 57
  • Is there a way to do a for loop here to return all the issues, not just the one a position 0. I.e. `for (singleIssue in allIssues)...`? – wmash Sep 22 '15 at 10:29
  • @WillAshworth an `Array.prototype.forEach()` or a regular `for (var i = 0; i < arr.length; i++)` loop will work. – Artyom Neustroev Sep 22 '15 at 10:35
  • @WillAshworth also, don't forget that your request is async: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Artyom Neustroev Sep 22 '15 at 10:37
  • Thank you! I'm not sure why that didn't work when I first did it – wmash Sep 22 '15 at 10:38