0

I am trying to get some information out of my JSON object and i keep getting an error on my console saying:

Uncaught TypeError: Cannot read property 'queue' of undefined". 

I have this json:

{
    "qmd_file": {
        "queue": "rr7323-psp",
        "name": "unicode",
        "full_path": "/devl/data/queue‌​s/psp/rr7323/unicode",
        "mtime": "2015-05-08T19:02:06.000-04:00"
    }
}

I want to be able to get queue name which is "rr7323-psp". Here is my code:

function searchFile (qid, filename) {
  var searchUrl = queue_web_services_base + "/q/v1/find.json?qgid="
                + encodeURIComponent(qid) + "&filename="
                + encodeURIComponent(filename);
  var token = encodeURIComponent(window.bpub.authToken);
  $.ajax({
    type: "GET",
    url: searchUrl,
    headers: {'Authorization' : 'Token token="' + token + '"'},
    success: function(json) {
      var jsonString = JSON.stringify(json);
      obj = JSON.parse(jsonString);
      console.log(obj.qmd_file[1].queue);
    }
  });

  return searchUrl;
}

self.searchForFile = function() {
  var queueGroup = prompt ("Please eneter Queue group" , "") 
  var fileName = prompt ("Please enter file name: " , "")
  console.log(searchFile(queueGroup, fileName));
}
3
  • Can you share an example of what the JSON you are loading looks like? Commented Aug 6, 2015 at 17:22
  • 1
    Hopefully this is what you are asking for: {"qmd_file":{"queue":"rr7323-psp","name":"unicode","full_path":"/devl/data/queues/psp/rr7323/unicode","mtime":"2015-05-08T19:02:06.000-04:00"}} Commented Aug 6, 2015 at 17:23
  • json object looks like this:Object {qmd_file: Object}qmd_file: Objectfull_path: "/devl/data/queues/psp/rr7323/unicode"mtime: "2015-05-08T19:02:06.000-04:00"name: "unicode"queue: "rr7323-psp" Commented Aug 6, 2015 at 17:23

3 Answers 3

1

You have obj.qmd_file[1].queue, but the qmd_file node is not an array. queue is a direct descendant attribute of obj.qmd_file. Try obj.qmd_file.queue.

Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to access the queue element in the JSON with this statement: obj.qmd_file[1].queue. This code means "get the 'queue' property of the 2nd object in the 'qmd_file' array in the 'obj' object".

However, in the JSON you posted, "qmd_file" is an object, not an array. Try instead using this code: obj.qmd_file.queue

Comments

1

You could fetch it as,

console.log(obj.qmd_file.queue);

queue is a property within the object fetched as obj.qmd_file and qmd_file is certainly not an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.