node.js - Handlebars 模板引擎循环问题

标签 node.js express mongoose handlebars.js

我想使用 hbs 从后端打印记录的用户 ID、姓名、电子邮件,我正在使用 MongoDB,但值没有打印,但循环正在工作。

<h1 class="display-1 text-center text-danger">WELCOME ADMIN</h1>

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">ID</th>
      <th scope="col">NAME</th>
      <th scope="col">EMAIL</th>
    </tr>
  </thead>
  <tbody>
    {{#each userArray}}
        <tr>
            <td>{{@key}}</td>
            <td>{{this._id}}</td>
            <td>{{this.name}}</td>
            <td>{{this.email}}</td>
        </tr>
        <script>
        
        var namee = {{this.name}}
        if (namee) {
        console.log("Name:", namee);
        }
    
        </script>
    {{/each}}
  </tbody>
</table> 

路由器:

var express = require('express');
var router = express.Router();
const userReg= require("../models/data")

router.get('/', async(req, res)=> {
 try{
  const userRegList = await userReg.find({});
  console.log("user count :"+userRegList.length);
  res.render('admin',{userArray:userRegList});
  console.log(userRegList)
 }catch(err){
  console.log(err);
 }
  });

  module.exports = router;

型号:

const mongoose = require("mongoose");

const personSchema=mongoose.Schema({
    name:String,
    email:String,
    password:String,
    repassword:String,
    birthday:Date,
    phonenumber:String,
    address:String 
});
var Person = mongoose.model("person",personSchema);
module.exports = Person//PersonalData

数据在控制台中正确打印,例如。使用 console.log(userRegList) 时,但当我在模板文件中使用 {{#each userArray.[0]}} 时,它没有显示。它确实显示了第一个userdata,但是为什么不打印完整的值?帮我解决这个问题!!

最佳答案

发生这种情况是因为,默认情况下,在 Handlebars >= v4.6.0 中,运行时禁止访问上下文对象的原型(prototype)属性和方法。 Handlebars 仍将循环遍历数组,但它将被阻止访问属性值。

您可以覆盖此设置,并且可以阅读更多内容 here ,但它的作用是保护您的系统免受试图利用安全漏洞的恶意行为者的侵害,因此我建议您不要覆盖它。

正如用户 @76484 所提到的,mongoose 有一个简单的 lean() 方法,您可以链接到您可能需要传递到 Handlebars View 的任何查询。这将返回 POJO与 Mongoose 文档类的实例相反,因此可以在 Handlebars View 中使用而不会遇到此问题。

By default, Mongoose queries return an instance of the Mongoose Document class. Documents are much heavier than vanilla JavaScript objects, because they have a lot of internal state for change tracking. Enabling the lean option tells Mongoose to skip instantiating a full Mongoose document and just give you the POJO.

试试这个:

const userRegList = await userReg.find({}).lean();

巧合的是,使用 lean() 将具有加快应用程序速度的额外好处,因为:

Under the hood, after executing a query, Mongoose converts the query results from POJOs to Mongoose documents. If you turn on the lean option, Mongoose skips this step.

关于node.js - Handlebars 模板引擎循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77071122/

相关文章:

c++ - node.js/[my own library/plugin] 如何在 v8 上运行?

javascript - res.render sweetalert 在 ejs 文件上

javascript - node.js 中不同的 module.exports 模式

node.js - 如何在 MongoDB 的正则表达式中搜索与给定字符串的匹配项?

node.js - Twitter 返回未经授权的用户时间线,但不返回搜索

javascript - 找不到node.js模块

node.js - express.json() 与正文解析器

javascript - 无法让 Node.js Express 代码在 Cloud 9 中显示 'Hello World'

MongoDB:将多个数值连接到字符串

node.js - 基于属性的 Mongoose 动态引用