输出用户集合nodejs

时间:2018-01-15 12:39:40

标签: node.js express handlebars.js

编辑1

用户模型和架构代码

const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');

var app = express();

if (app.get('env') === 'production') {
  mongoose.connect(process.env.MONGODB_URI, { useMongoClient: true });
} else {
  mongoose.connect('mongodb://localhost/pol-development', { useMongoClient: true });
}

var db = mongoose.connection;
mongoose.Promise = global.Promise;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log("Connection has been established");
});

var UserSchema = mongoose.Schema({
  schoolName: String,
  schoolAddress: String,
  schoolAddress2: String,
  city: String,
  zipCode: String,
  addressCheck: Boolean,
  postalAddress: String,
  postalCity: String,
  postalZipCode: String,
  telephone: String,
  fax: String,
  email: { type: String, required: true, unique: true },
  password: String,
  schoolType: String,
  schoolDistrict: String,
  schoolRegion: String,
  curriculum: String,
  participationBefore: Boolean,
  participationYears: String,
  directorName: String,
  directorTelephone: String,
  directorEmail: String,
  directorAttendanceRehersal: Boolean,
  directorAttendanceEvent: Boolean,
  schoolRepresentativeName: String,
  schoolRepresentativeTelephone: String,
  schoolRepresentativeEmail: String,
  schoolRepresentativePosition: String,
  schoolRepresentativeOtherPosition: String,
  schoolRepresentativeTShirt: String,
  schoolRepresentativeTutorMentor: String,
  admin: { type: Boolean, default: false },

  // admin fields
  directorAttendanceRehersal: Boolean,
  directorCompetitionEvent: Boolean,
  attendanceRehersal: Boolean,
  attendanceEvent: Boolean,
});

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.comparePassword = function(candidatePassword, hash, callback) {
  bcrypt.compare(candidatePassword, hash, function(err, isMatch) {
    if(err) throw err;
    callback(null, isMatch);
  });
}

// get all users from collection
db.collection("users").find({}).toArray(function(err, result) {
  if (err) throw err;
  console.log(result);
  db.close();
});

原始

我有我的UsersSchema,我有一个用户集合,但我想循环并输出所有用户。

目前我有

{{#each user}}
  <tr>
    <td><a href="/dashboard/users/{{_id}}">{{schoolName}}</a></td>
    <td>{{city}}</td>
    <td>{{email}}</td>
    <td>{{schoolRepresentativeName}}</td>
    <td>{{schoolRepresentativeTutorMentor}}</td>
  </tr>
{{/each}}

但只输出您登录的用户。

以下是视图中出现的内容

enter image description here

路线目前是这样的

router.get('/dashboard/users', ensureAuthenticated, (req, res) => {
  res.render('dashboard/users/index.hbs', {
    pageTitle: 'Users'
  });
});

如何输出集合中的所有用户?

1 个答案:

答案 0 :(得分:0)

您是否尝试使用名为Windows("Sales wk02.xlsx").Activate Range("C2").Select ActiveCell.FormulaR1C1 = _ "=VLOOKUP(RC[-2],'[Daily_Report BOE.xlsx]Nocturne'!R4C1:R105C13,13,0)" MongoDB 查询,该查询允许您查找集合的所有元素? db.collection.find()

编辑1 这首先不起作用,因为您实际上并未查询 MongoDB架构,但您只是粘贴了我的示例代码。

我可以看到您实际上已将模式定义为db.collection.find(),因此在我的代码中替换它,导致:

var UserSchema

现在问题是如何捕获此查询,您需要在html页面上显示它。在查询中查看dbo.collection("UserSchema").find({}).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); =&gt; console log您应该能够在命令提示符中看到记录的所有用户信息。这对你有用吗?

MongoDB with NodeJS Examples.