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!