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

本文标签: javascriptobject SequelizeInstance being passStack Overflow