Nodejs查询结果不显示?

时间:2018-01-23 02:09:28

标签: javascript node.js mongodb handlebars.js

我将MongoDB与Mongoose一起使用来创建数据对象,但我在网页上显示这些对象时遇到了问题。我将getAllUsers查询添加到了我的users.js文件中。然后我使用把手在我的模板网页上显示用户列表。

我的桌面上有用户,但我看不到任何结果。我错过了什么?

以下完整代码。谢谢!

Users.js:

var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');

var UserSchema = new mongoose.Schema({
  email: {
    type: String,
    index: true,
    required: true,
    trim: true,
    minlength: 1,
    unique: true,
  },
  password: {
    type: String,
    require: true,
    minlength: 6
  },
  username: {
    type: String,
    require: true
  },
  tokens: [{
    access: {
      type: String,
      required: true
    },
    token: {
      type: String,
      required: true
    }
  }]
});

var User = module.exports = mongoose.model('User', UserSchema);

module.exports.createUser = function(newUser, callback){
    bcrypt.genSalt(10, function(err, salt) {
        bcrypt.hash(newUser.password, salt, function(err, hash) {
            newUser.password = hash;
            newUser.save(callback);
        });
    });
}

module.exports.getUserByEmail = function(email, callback){
  var query = {email: email};
  User.findOne(query, callback);
}

module.exports.getUserById = function(id, callback){
  User.findById(id, callback);
}

module.exports.getAllUsers = function(callback){
  User.find({}, function(err, users) {
    if(err){
      res.send('error');
      next();
    }
    res.json(users);
  });
}

module.exports.comparePassword = function(candidatePassword, hash, callback){
  bcrypt.compare(candidatePassword, hash, function(err, isMatch){
    if(err) throw err;
    callback(null, isMatch);
  });
}

Users.handlebars:

<div class="main-container">
  <section>
    <div class="container">
      <div class="row mb64 mb-xs-24">
        <div class="col-sm-12 text-center">
          <h3>users</h3>
          <p class="lead"></p>
        </div>
      </div>
    </div>
  </section>
  <section>
    <div class="container">
      <div class="row">
        <div class="col-md-8 col-sm-10 col-sm-offset-1 col-md-offset-2">
            <div class="horizontal-tile">
              {{#each users}}
              <div class="tile-left">
                <a href="#">
                  <div class="background-image-holder">
                    <img alt="image" class="background-image" src="img/project-single-1.jpg">
                  </div>
                </a>
              </div>
              <div class="tile-right bg-secondary">
                <div class="description">
                  <h4 class="mb8">{{this}}</h4>
                  <h6 class="uppercase">
                    Graphic Design
                  </h6>
                  <p>
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                  </p>
                </div>
              </div>
            </div>
            {{/each}}
        </div>
      </div>
    </div>
  </section>
</div>

编辑:

我的路线文件夹中的users.js:

// get users
router.get('/', function(req, res) {
  res.render('users');
});

1 个答案:

答案 0 :(得分:0)

重新构建您的方法,以便它返回Users数组而不响应(它无论如何都无法访问res):

module.exports.getAllUsers = function(callback){

  return User.find({}, function(err, users) {

    return callback(err, users);

  });

}

users.js路线内:

const Models = require('/path/to/your/models');

...
...
...

router.get('/', function(req, res) {

  Models.User.getAllUsers(function(err, users) {

    if(err) {
        res.send('error');
        next();
    }

    res.render('users', users);

  });

});