MongoDB - 无法从数据库中读取数据

时间:2015-07-13 18:56:47

标签: javascript node.js mongodb model-view-controller

嗨,我是新手,我正面临着一个问题。我正在做一个rideshare cab应用程序,并在jade文件中获得Cannot read property 'length' of undefined error

我的玉文件如下: -

html
  body
    h1 Passengers List
    each user in users
      script(type='text/javascript').
        function checkboxlimit(checkgroup, limit){
        var checkgroup=checkgroup
        var limit=limit
        for (var i=0; i<checkgroup.length; i++){
        checkgroup[i].onclick=function(){
        var checkedcount=0
        for (var i=0; i<checkgroup.length; i++)
        checkedcount+=(checkgroup[i].checked)? 1 : 0
        if (checkedcount>limit){
        this.checked=false
        }
        }
        }
        }
      p Select your passenger below:
      form#world(name='man')
        input(type='checkbox', name='dude')
        |  li {passenger.name}
        br
      script(type='text/javascript').
        //Syntax: checkboxlimit(checkbox_reference, limit)
        checkboxlimit(document.forms.man.dude, 4)

在这个文件中,我试图显示所有用户,并让司机选择四名乘客..

完成后,我将创建一个新的玉器文件,以显示与驱动程序相关的驱动程序和乘客

以下是我的serverjs片段: -

app.post('/passenger',function(req,res){
        var user = new User({profile:{name:req.body.uname},
        type:"passenger",
        phone_no:req.body.contact,
        origin:{city:req.body.pick},
        destination:{city:req.body.drop}
      //  email:req.body.email,
    //    password:req.body.password
    })
         user.save();
       //  passengers.push(user)
       //  console.log(passengers)

        res.render('checkbox', {user: user});
       // res.redirect('/checkbox')
    });   

app.get('/checkbox',function(req,res){
        res.render('checkbox');
    });

我的模型如下: -

var userSchema = new mongoose.Schema({
  email: { type: String, unique: true, lowercase: true },
  password: String,
  facebook: String,
  google: String,
  origin: {city: String, state: String},
  destination: {city: String, state: String},
  price: Array,
  time: Date,
  phone_no: String,
  accepted:String,
  tokens: Array,
  type: String,
  profile: {
    name: { type: String, default: '' },
    gender: { type: String, default: '' },
    location: { type: String, default: '' },
    website: { type: String, default: '' },
    picture: { type: String, default: '' }
  },

  resetPasswordToken: String,
  resetPasswordExpires: Date
});

我似乎无法理解代码中的错误。请帮助我

由于

1 个答案:

答案 0 :(得分:0)

var x = {};
x.someProperty; //this is undefined
x.someProperty.length; // Cannot read property 'length' of undefined

所以,你试图得到一个未定义的属性。尝试找出您尝试获取的代码的哪一部分length

function checkboxlimit(checkgroup, limit) {注意这一行,将其undefined

var checkgroup=checkgroupvar limit=limit毫无意义。

=)