0

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;

1 Answer 1

2

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
    })
  });
});
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.