javascript - object SequelizeInstance being pass - Stack Overflow

I am confused and I hope you can help me.I am building an app in node.js using express.js and sequeli

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
Add a ment  | 

2 Answers 2

Reset to default 3

Try 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
  }
]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744702428a4588850.html

相关推荐

  • javascript - object SequelizeInstance being pass - Stack Overflow

    I am confused and I hope you can help me.I am building an app in node.js using express.js and sequeli

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信