0

Narrowed it down to this:

$.getJSON('/drinks/getColors', function(items) {
    colors = items;
});

$.each(colors, function(index2, value2) {
    if (value.name == value2.name) {
        curColor = value2.color;
    }
});

I use this information later so I store it locally, but sometimes it is undefined, other times it's fine. What makes it so unreliable?

The problem it seems to have is that colors is sometimes undefined. Is there a timing issue with working on the data before it's done being received?

^===EDIT===^

Gonna try and give you all the necessary info without just pasting my whole project.

So on pageload of my drinks/find pages, I run a GET request to my database to get the drinks details and to populate the page.

$(document).ready(function() {
 loadDrink(curDrink);
});

From there, the function gets the data:

$.getJSON(address, function(item) {
 #Work with data, update pages, loops, etc
}

Often, but not always, I will get a TypeError: a is undefined error from Jquery, but refreshing a few times eventually gets it to work.

Here is the route that calls on my database:

router.get('/find/details/:name', function (req,res){
 var db = req.db;
 nameToSearch = req.params.name.toLowerCase();
 db.collection('recipes').findOne({name: nameToSearch}, function (err, result) {
    res.send(result);
 });
});

I'm not exactly sure what the problem is. There are spaces in some of my queries (going to fix), but this error also happens with single word searches so I don't think that's the cause of the problem.

I'm guessing it has something to do with the page trying to work on data that hasn't been received yet. But the work being done is also a callback on $.getJSON, so I thought that wouldn't be an issue.

If you guys spot anything that would be super appreciated, I can also provide more info if needed, wasn't sure what was relevant.

Thanks!

xHocquet
  • 362
  • 2
  • 11
  • You haven't posted enough code. – Pointy Apr 05 '15 at 16:00
  • What else would help? – xHocquet Apr 05 '15 at 16:01
  • Well for starters the code that involves a variable called "a". – Pointy Apr 05 '15 at 16:02
  • There is no variable a. A is probably referring to an itterable that jquery creates. – xHocquet Apr 05 '15 at 16:03
  • Well the only thing to do when you're getting an error from inside the library is to debug your page with a non-minified version of it so that you can trace up the stack and find out where in *your* code things are going wrong. There's no general answer; jQuery includes very little code to validate parameters you pass to it, so there are a zillion ways things can go wrong in that way. – Pointy Apr 05 '15 at 16:06
  • Well what leads me to believe it's something on my end is that it works sometimes. I can recreate it regularly by refreshing by clearing the cache, and then if I refresh normally it'll work after a couple times. – xHocquet Apr 05 '15 at 16:11

2 Answers2

0

The first thing that I would start with is look at the GET request to guarantee that data is actually being returned each time. As for the 'a is undefined` error, I am assuming that you are using a minified version of JQuery. Try using a non-minified version, might point you in the right direction assuming the issue is client side.

  • As I thought, the error stems from the `$.each` function. I checked all the data right before the iteration and it all seems to be there. The only one that could not be there gets checked for length before trying to go over it. – xHocquet Apr 05 '15 at 16:17
  • @xHocquet Just so you know: we can't see the `$.each`, we cant see how the returned data looks or that you're checking for length. This is all info that is required to help debug. The code you've posted in your question doesn't give us much: `$(document).ready(function() { //some code here for different things })`, ok, the two lines of code looks good. :) – Mackan Apr 05 '15 at 19:08
  • Added the $.each. It just seems that sometimes `colors` isn't there when I try to work on it. – xHocquet Apr 05 '15 at 19:18
0

It was a completely unrelated issue that I badly explained in here. Just trying to work off of data after a $.get, not inside the callback. Keep that code asynchronous!

xHocquet
  • 362
  • 2
  • 11