javascript - Sails JS - How to Work With Specific Model Attributes from the Associated Controller -
i'm learning javascript, node , sails, , have basic newb (i think) question couldn't find answer elsewhere.
in controller, i'm trying access specific model attribute , create array includes each separate entry use in view. while can access records in model in menu/index view, can't seem work specific attribute directly in model.
here's controller code in question:
var menucontroller = module.exports = { manager: function(req, res) { var userid = req.session.user.id; sails.log("searching foods user: "+ userid); menu.find({ where: { userid: { equals: userid }}}).exec(function(err, records) { if(records && records.length == 0) { sails.log("no foods found user: " + userid); sails.log(err); return res.view("menu/index"); } else { var foodcategories = records.food_category; sails.log(foodcategories); var array = foodcategories.split(','); var foodcatarray = []; (i = 0; < array.length; i++) { foodcatarray.push(array[i].touppercase()); var uniquecats = foodcatarray.filter(function (item, i, ar) { return ar.indexof(item) === i; }); var sortedcats = uniquecats.sort(); sails.log(sortedcats); } return res.view("menu/index", {foods: records, sortedcats: sortedcats}); } }); },
and here's model in question:
var menu = module.exports = { attributes: { userid: { required: true, type: 'integer', min: 1 }, food_category: { type: "string", required: false, min: 1, max: 200 }, food_name: { type: "string", required: true, min: 1, max: 200 }, food_cost: { type: "string", required: true, min: 1, max: 10 }, food_description: { type: "string", required: true }, img_path: { type: "string", required: true }, img_name: { type: "string", required: true }, img_name_crypt: { type: "string", required: true } } };
right line:
var foodcategories = records.food_category;
creates undefined value in sails log when load menu/index view. can point me in right direction?
your records variable array of objects.
move code inside loop , use
records[i].food_category
you don't need var array ... in loop use records.lenght instead
Comments
Post a Comment