I want to print logged users id,name,email from backend using hbs I am using MongoDB but the values are not printing but loop is working.
我想打印登录的用户id,姓名,电子邮件从后端使用HBS我正在使用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>
router:
路由器:
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;
model :
型号:
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
The data is printing properly in the console, eg. when using console.log(userRegList)
but it's not showing when I use {{#each userArray.[0]}}
in the template file. It shows the first userdata
without fail, but then why does the complete value not print? Help me with this!!
数据在控制台中打印正确,例如。当使用console.log(UserRegList)时,当我在模板文件中使用{{#each userArray.[0]}}时没有显示。它毫无错误地显示了第一个用户数据,但是为什么没有打印完整的值呢?帮帮我!!
更多回答
{{#each userArray.[0]}} <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}} this is working but it is only showing first value in the DB
{{#每个用户阵列.[0]}}{{@key}}{{this._id}} | {{this.name}} | {{this.mail}} |
var Namee={{this.name}}if(Namee){sole.log(“name:”,Namee);}脚本>{{/each}}这是正常的,但它只显示数据库中的第一个值
I believe your userRegList
is not a regular JavaScript array of objects, but a Mongoose construct containing Mongoose Documents. I believe you can append your .find({})
query with [.lean()
](mongoosejs.com/docs/api/query.html#Query.prototype.lean()) in order to get back a result of plain old JavaScript.
我相信您的userRegList不是一个常规的对象的JavaScript数组,而是一个包含Mongoose文档的Mongoose构造。我相信您可以在.find({})查询后面附加[.Lean()](mongoosejs.com/docs/api/query.html#Query.prototype.lean()),以便返回普通的旧式JAVASCRIPT。
优秀答案推荐
This happens because, by default, in Handlebars >= v4.6.0 accessing prototype properties and methods of the context object is forbidden by the runtime. Handlebars will still loop through the array but it will be blocked from accessing the property values.
这是因为在默认情况下,在Handlebar>=v4.6.0中,运行时禁止访问上下文对象的原型属性和方法。Handlebar仍将在数组中循环,但将被阻止访问属性值。
You can override this setting and you can read more here, but it's there to protect your system from malicious actors trying to exploit security vulnerabilities so I would advise you not to override it.
您可以覆盖此设置,您可以在此处阅读更多内容,但它的存在是为了保护您的系统免受试图利用安全漏洞的恶意攻击者的攻击,因此我建议您不要覆盖它。
As mentioned by user @76484 mongoose has a simple lean()
method you can chain to any of your queries that you may need to pass to your handlebars views. This returns POJO as opposed to instances of the Mongoose Document Class and can therefore be used in your Handlebars views without encountering this issue.
正如User@76484所提到的,Mongoose有一个简单的Lean()方法,您可以将其链接到您可能需要传递给Handlebar视图的任何查询。这将返回POJO,而不是Mongoose Document类的实例,因此可以在您的Handlebar视图中使用,而不会遇到这个问题。
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.
Try this instead:
试着这样做:
const userRegList = await userReg.find({}).lean();
Coincidentally, using lean()
will have the added benefit of speeding up your application because:
巧合的是,使用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.
更多回答
It works thank you bro.
很管用,谢谢你,兄弟。
我是一名优秀的程序员,十分优秀!