2
回答
Mongoose中model.find()的查询结果报“Cannot read property 'userName' of undefined”

代码如下:

//model/model.js
var mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
  userName: String,
  email: String,
  phone: { type: Number, min: 11 }
});

mongoose.connect('mongodb://localhost/form');

module.exports = userSchema;

//router/list.js
var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();
var userSchema = require('../model/model');
var userInfo = mongoose.model('userInfo', userSchema);

router.get('/list', function(req, res) {
  res.render('list', { title: 'Express', data: userInfo.find() });
});

module.exports = router;

//views/list.jade
extends layout

block content
  h1= title
  p Welcome to #{title}
  ul.list
    each val, index in data
      li
        strong= val.userName
          //span= val.email
          //strong= val.phone



最后浏览器中显示“Cannot read property 'userName' of undefined”
举报
发帖于4年前 2回/1K+阅
顶部