cphill
cphill

Reputation: 5924

ExpressJS - Sequelize - Query Results Not Populated in Object

I have a route set up that conducts the following sql query:

Executing (default): SELECT `id`, `pattern`, `color`, `imageUrl`, `imageSource` FROM `images` AS `images` WHERE `images`.`pattern` = 'solid' AND `images`.`color` = 'navy-blue';

which when tested on my local database works and provides results, but I can't seem to populate my image object to then be used within my view. I feel like I'm passing it as a function parameter in the wrong place, but I'm not sure and my console.log(image); is not coming back with a response.

router.get('/:pattern/:color/result', function(req, res, image){

    console.log(req.params.color);
    console.log(req.params.pattern);

    Images.findAll({ 
        where: {
            pattern: req.params.pattern,
            color: req.params.color
        },
        attributes: ['id', 'pattern', 'color', 'imageUrl', 'imageSource']
    }),
        console.log(image);
        //console.log(doc.descriptions_id);
        res.render('pages/result.hbs', {
            pattern : req.params.pattern,
            color : req.params.color,
            image : image
        })
});

Here is my model:

var Sequelize      = require('sequelize');
var sequelize = new Sequelize('db', 'admin', 'test', {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql'
});


var Images = sequelize.define('images', {
    pattern: {
        type: Sequelize.STRING,
        field: 'pattern'
    },
    color: {
        type: Sequelize.STRING,
        field: 'color'
    },
    imageUrl: {
        type: Sequelize.STRING,
        field: 'imageUrl'
    },
    imageSource: {
        type: Sequelize.STRING,
        field: 'imageSource'
    },
    description_id: {
        type: Sequelize.INTEGER,
        field: 'description_id'
    }
});

module.exports = Images;

Upvotes: 0

Views: 428

Answers (1)

Rudolf Manusachi
Rudolf Manusachi

Reputation: 2356

You should use Promises

router.get('/:pattern/:color/result', function(req, res, image){
  console.log(req.params.color);
  console.log(req.params.pattern);

  Images.findAll({ 
    where: {
        pattern: req.params.pattern,
        color: req.params.color
    },
    attributes: ['id', 'pattern', 'color', 'imageUrl', 'imageSource']
  }).then(function(image){
    console.log(image);
    //console.log(doc.descriptions_id);
    res.render('pages/result.hbs', {
        pattern : req.params.pattern,
        color : req.params.color,
        image : image
    })
  });
});

Upvotes: 2

Related Questions