Priya  Balakrishnan
Priya Balakrishnan

Reputation: 51

displaying values from sequelize query using handlebars

I have a Cart model with product_price and quantity. I am writing a sequelize query to calculate the total as price * quantity Here's my query

db.Cart.findAll({

    attributes: {include: [[db.sequelize.condition(db.sequelize.col('cart_quantity'), '*', db.sequelize.col('price')),'tot']]}

})

Sequelize documentatoin says to access the value of 'tot' we use instance.get('tot'). So in the results console.log(item.get('tot') displays correct value

.then(function(dbCart) { //We have access to the products as an argument inside of the callback function

  dbCart.forEach(function(item){
    console.log(item.get('tot'));
  });
  var hbsObject = {
    products: dbCart,
  };
res.render("shop/cart", hbsObject);
});

});

Now when I try to access the value in my carts.handlebars file I am unable to display or access the value of 'tot' {{# each products }} {{tot}} or {{Cart.tot}} or {{products.tot}} or {{products.get('tot')}} yields no value {{/each}} How do I access and display the value of 'tot' in handlebars? Thank you

Upvotes: 3

Views: 2433

Answers (3)

Keyvan
Keyvan

Reputation: 121

EDIT 08/02/2022 :

If you want to access values from your models just like you would any other JSON object, you need to serialize them.

I found the quickest and easiest way to do that is with sequelize-json-schema and sequelize-serialize

In the file where you define your models :

import sjs from 'sequelize-json-schema'

// ... define models with sequelize.define()
const Person = sequelize.define('Person', {name: DataTypes.STRING});

// generate JSON schemas
const schemas = sjs.getSequelizeSchema(sequelize).definitions

// ^ note that I'm only exporting "definitions" because I don't need the rest of the JSON schema

//make sure to export schemas to use it in your app
export {
  sequelize,
  schemas,
  Person
  //...
}

Then, when you need to use your models :

//import your models AND the schemas
import { Person, ..., schemas } from '../yourmodels.js'

//import the serialize method from sequelize-serialize
import serialize from 'sequelize-serialize'

//use it to serialize your result
Person.findOne({where: {foo: 'bar'}}).then(model => {
  const person = serialize(model, schemas.person)
  // you can now use 'person' in your view and access its properties
});

Upvotes: 0

Denis Francia Karell
Denis Francia Karell

Reputation: 783

Try:

{{#each users}}
    {{#dataValues}}
        <p>Username: {{username}}</p>
        <p>Email: {{email}}</p>
    {{/dataValues}}
{{/each}}

Or try:

{{#each users}}
    <p>Username: {{this.dataValues.username}}</p>
    <p>Email: {{this.dataValues.email}}</p>
{{/each}}

Upvotes: 3

C.J. Windisch
C.J. Windisch

Reputation: 309

As far as I can tell you can only access the result of the query with item.get('tot'), but since you can't call functions from handlebars you need a work around. One way to do it is to add a handlebars helper that just wraps the .get() function.

const Handlebars = require('handlebars');
Handlebars.registerHelper('sequelizeGet', function(obj, col) {
  return obj.get(col);
}); 

And in the handlebars template access that helper like:

<span>{{sequelizeGet item 'tot'}}</span>

Upvotes: 1

Related Questions