我的javascript代码第52行的错误,我无法弄明白

时间:2014-06-26 01:41:06

标签: javascript mongodb

我在我的appery.io应用程序中使用以下javascript代码。我一直收到一个错误,说明如下:

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) {

请帮我识别错误。我正在尝试从3个集合中提取数据,通过“用户”集合中的_id同步它们,然后输出用户配置文件类型信息。

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 on all users
for (var i=0;i<all_users.length;i++)
{
    // call function to search for user profile and then add first name to current user            item
    getProfile(all_users[i]._id, all_users[i]);
    // call function to search for user status and then add last status to current user   item  
    getstatus(all_users[i]._id, all_users[i]);
    getphoto(all_users[i]._id, all_users[i]);
}

// function get user item and user id and find user profile by its id and update it
function getProfile(id,curUser)
{
    var found = false;
    for (var i = 0; i < all_profiles.length; i++) {
        // if cur user id = profile id assign profile name to the user
        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 get user item and user id and find user status by its id and update 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 full user data updated wih status and first name
response.success(JSON.stringify(all_users), "application/json");

1 个答案:

答案 0 :(得分:1)

这意味着这是未定义的:

all_photo[i].the_user

因为它未定义,所以它绝对没有属性_id,因为未定义的对象没有属性,因为它们是未定义的。

这是否定义了问题的根源?

-

使用浏览器控制台 - 它有助于:

console.log(all_photo);

然后,您可以查看该对象发生了什么以及在eval之后它具有哪些属性。