ga-wdi-exercises / pbj-project3

[project]
0 stars 2 forks source link

Can't access queried Object #39

Closed omarimayerswalker closed 9 years ago

omarimayerswalker commented 9 years ago

When I console.log(vendor) it returns the object as expected but if I console.log(vendor.id) I get undefined.

var Vendor = require("../db/connection").models.Vendor;
var Market = require("../db/connection").models.Market;

module.exports = {
    marketSearch: function(req, res){
        var userSub = req // this is working
        console.log(userSub + " " + Vendor) // this returns Soupergirl + [object SequelizeModel:vendor]
        Vendor.findAll({ where: {name: userSub }}).then(function(vendor){
            console.log(vendor.id)
        })
    }
}

console.log(vendor) returns:

selection_002

RobertAKARobin commented 9 years ago

It's because findAll returns an array of records, even if that array contains one thing. If you just want the ID of the first one, you'd need vendor[0].id. Make sure your variables names make sense!