查找转弯值未定义

时间:2018-09-11 16:17:47

标签: node.js mongodb express mongoose

我在这里的路线应显示所有房间,每个房间有8张床,我正在尝试 检查是否所有床都被占用。

正在发生的事情是,在函数中未找到返回值下方的查找之后

在这里您可以看到房间路线:

// Rooms Route
app.get('/rooms',function(req,res){
// ansArr = [];//arr for taken bed in room
var count, rNum, bNum;
var arrSortRooms = [], i=0;//arr to sort the boys and girls room
Hotelroom.find({},function(err, allRooms) {
    if(err){
        console.log('@@@@@@@@@@ Error @@@@@@@@');
        console.log(err);
        console.log('@@@@@@@@@@@@@@@@@@@@@@@@@');
    }
    else{
        // find how much beds open in room and put the answer in arrayy
        allRooms.forEach(function(room){ 
            getnum(room);
            arrSortRooms.push(room);
        });
        console.log('@@@@@@ Camp-Rendered @@@@@@');
        arrSortRooms = arrSortRooms.sort(function(a, b){return b.room_num 
        a.room_num });
        res.render('rooms.ejs',{arrSortRooms:arrSortRooms,ansArr:ansArr});

    }
});
});

这是计算每个房间的已用床位的功能

function getnum(room){
    count=0;
    BedInRoom.find({room_num:room.room_num},function(err,rm){
        if(err){

        }
        else{
            rm.forEach(function(r){
                count++;
                return count;
            });
        }
    });
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

问题解决了。

房间路线:

//Rooms Route
app.get('/rooms',function(req,res){
var count, rNum, bNum;
var arrSortRooms = [], i=0,c; //arr to sort the boys and girls 
room
var ansArr = [];//arr that contain how much beds are taken in 
//every room
BedInRoom.find({},function(err,allbeds){
 if(err){
     console.log(err);
 }
 else{
     Hotelroom.find({},function(err, allRooms) {
         if(err){
            console.log('@@@@@@@@@@ Error @@@@@@@@');
            console.log(err);
            console.log('@@@@@@@@@@@@@@@@@@@@@@@@@');
         }
         else{
            //find how much beds taken in every room
            allRooms.forEach(function(room){
                c=setCount(allbeds,room)
                ansArr.push(c);
                arrSortRooms.push(room);
            });
            console.log('@@@@@@ Rooms-Rendered @@@@@@');
            arrSortRooms = arrSortRooms.sort(function(a, b) 
            {return b.room_num - a.room_num });
            res.render('rooms.ejs', 
            {arrSortRooms:arrSortRooms,ansArr:ansArr});
             }
         });
      }
    });
});

功能:

/////// function //////////////////////////////////////////

function setCount(allbeds,room){//count taken beds per room
count =0;
allbeds.forEach(function(b){
    if(b.room_num==room.room_num)
    {
        count++
    }
});
return room.room_num+","+count;
//return the room and the taken beds
}