I've encountered an error in my appery.io app while using the following javascript code. The error message reads as follows:
6/25/2014 9:37:35 PM: Script All_Users_Data: TypeError: Cannot read property '_id' of undefined ( @ 52 : 33 ) -> if (all_photo[i].the_user._id == id) {
I need assistance in debugging this issue. My objective is to retrieve data from 3 collections, synchronize them based on _id from a 'users' collection, and then display user profile information.
var all_users = eval(DatabaseUser.query('52895ecce4b056c5e94f34f9'));
var all_profiles = eval(Collection.query('52895ecce4b056c5e94f34f9', 'profile'));
var all_status = eval(Collection.query('52895ecce4b056c5e94f34f9', 'Status'));
var all_photo = eval(Collection.query('52895ecce4b056c5e94f34f9', 'photo'));
// loop through all users
for (var i=0;i<all_users.length;i++)
{
// find user profile by id and add first name to current user item
getProfile(all_users[i]._id, all_users[i]);
// find user status by id and add last status to current user item
getstatus(all_users[i]._id, all_users[i]);
getphoto(all_users[i]._id, all_users[i]);
}
// function that retrieves user profile by id and updates it
function getProfile(id,curUser)
{
var found = false;
for (var i = 0; i < all_profiles.length; i++) {
// assign profile name to the user if user id matches profile id
if (all_profiles[i].the_user._id == id)
{
curUser.firstName = all_profiles[i].firstName;
curUser.university = all_profiles[i].university ;
found = true;
}
}
if (!found)
{
curUser.f_name = "";
}
}
// function that retrieves user status by id and updates it
function getstatus(id, curUser) {
var found = false;
for (var i = 0; i < all_status.length; i++) {
if (all_status[i].the_user._id == id) {
curUser.status = all_status[i].status;
found = true;
}
}
if (!found) {
curUser.status = "";
}
}
function getphoto(id, curUser) {
var found = false;
for (var i = 0; i < all_photo.length; i++) {
if (all_photo[i].the_user._id == id) {
curUser.photo = all_photo[i].photo;
found = true;
}
}
if (!found) {
curUser.photo = "";
}
}
// return complete user data updated with status and first name
response.success(JSON.stringify(all_users), "application/json");