3

I am beginning to learn SequelizeJs however I encounter a small issue: I have a model which is defined as such:

var ex_table= sequelize.define("ex_table", {
   concept1: Sequelize.STRING(5),
   concept2: Sequelize.STRING(80),
   concept3: Sequelize.STRING,
   weight: Sequelize.INTEGER,
   value: Sequelize.DECIMAL(20,2)
}, {
   tableName: "samble_table"});

Retrieving a single row from the table works, I do it like this:

ex_table
  .find({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
  .complete(function (err, result) {
    if (!!err) {
      console.log("An error occurred while creating the table:", err);
    } else {
      console.log(result.values);
    }
  })

This gives me one row of data, which is as expected. However when I'm trying to retrieve multiple rows like this:

ex_table
  .findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
  .complete(function (err, result) {
    if (!!err) {
      console.log("An error occurred while creating the table:", err);
    } else {
      console.log(result.values);
    }
  })

Here I get underfined, any ideas how to get the multiple rows?

2
  • Isn't result an array in the findAll case? Commented May 5, 2014 at 14:37
  • @barry-johnson Indeed it is an array, I was confused as I thought I had to do something with result.rows or result.values, I found out the solution, I will answer my own question. Commented May 5, 2014 at 23:45

1 Answer 1

2

I found out, that there are at least 2 ways to get all the rows: As commented by @barry-johnson, result is an array so we go over the array like any other:

ex_table
  .findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
  .complete(function (err, result) {
    if (!!err) {
      console.log("An error occurred while creating the table:", err);
    } else {
      for (var i=0; i<result.length; i++) {
           console.log(result[i].values);
           }
    }
  })

or there is this interesting line on raw queries in the Models part of Sequelize documentation:

Sometimes you might be expecting a massive dataset that you just want to display, without manipulation. For each row you select, Sequelize creates a DAO, with functions for update, delete, get associations etc. If you have thousands of rows, this might take some time. If you only need the raw data and don't want to update anything, you can do like this to get the raw data.

As I wanted to see the raw data, this is what we need, so we can get the multiple rows like this:

ex_table
  .findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})}, {raw:true})
  .complete(function (err, result) {
    if (!!err) {
      console.log("An error occurred while creating the table:", err);
    } else {
           console.log(result);
    }
  })

Hopefully this will help someone else as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.