admin管理员组文章数量:1391969
I am confused and I hope you can help me. I am building an app in node.js using express.js and sequelize for my ORM. I have created a model with the following code:
"use strict";
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
username: DataTypes.STRING
});
return User;
};
My route looks like this:
router.get('/', function (req, res) {
models.User.findAll({ }).then(function(users) {
res.render('index', {
title: 'Project Insight',
users: users
});
});
})
And I am using EJS for my templates:
<ul>
<% for(var i=0; i<users.length; i++) {%>
<li><%= users[i] %></li>
<% } %>
</ul>
The output I getting in my views is this:
[object SequelizeInstance]
[object SequelizeInstance]
[object SequelizeInstance]
[object SequelizeInstance]
I want to be able to pull the names from the database and I am confused about why this is giving that result. Please let me know if there is any info you need from me. I am new to all of this and I am at a standstill. Thanks, in advance, for all your help.
I am confused and I hope you can help me. I am building an app in node.js using express.js and sequelize for my ORM. I have created a model with the following code:
"use strict";
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
username: DataTypes.STRING
});
return User;
};
My route looks like this:
router.get('/', function (req, res) {
models.User.findAll({ }).then(function(users) {
res.render('index', {
title: 'Project Insight',
users: users
});
});
})
And I am using EJS for my templates:
<ul>
<% for(var i=0; i<users.length; i++) {%>
<li><%= users[i] %></li>
<% } %>
</ul>
The output I getting in my views is this:
[object SequelizeInstance]
[object SequelizeInstance]
[object SequelizeInstance]
[object SequelizeInstance]
I want to be able to pull the names from the database and I am confused about why this is giving that result. Please let me know if there is any info you need from me. I am new to all of this and I am at a standstill. Thanks, in advance, for all your help.
Share Improve this question asked Jan 22, 2015 at 19:23 SchnaarsSchnaars 472 silver badges11 bronze badges 3-
1
My first guess is that you need to use
users[i].username
– sites Commented Jan 22, 2015 at 19:33 - ^ This is correct and you should answer it. Also, this would work: users[i].dataValues.username Thanks for your help. – Schnaars Commented Jan 22, 2015 at 20:10
-
Using
dataValues
attr is not remended. The best idea to send objects into a templating system is to use the properties directly, or use the.toJSON()
method. Check N.B. – Dan Rocha Commented Jan 22, 2015 at 22:01
2 Answers
Reset to default 3Try to change EJS template:
<ul>
<% for(var i=0; i<users.length; i++) {%>
<li><%= users[i].username %></li>
<% } %>
</ul>
Might be helpful for other people:
model.findAll({where}).
.then(res => {
return res.map(row => {
return row.dataValues
});
})
Which will return list of objects that looks like:
[
{
column1: value1,
column2: value2,
...
columnN: valueN
}
]
本文标签: javascriptobject SequelizeInstance being passStack Overflow
版权声明:本文标题:javascript - object SequelizeInstance being pass - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744702428a2620636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论