Sails JS - How to work with specific model attributes from a linked controller

I'm learning Javascript, Node and Sails, and have a basic newb question (I think) that I couldn't find an answer elsewhere.

In the controller, I try to access a specific attribute of the model and then create an array containing each individual record to use in the view. While I can access the records in my model in my menu / index view, I cannot work with any particular attribute directly in the model.

Here's the controller code:

var MenuController = module.exports = {

manager: function(req, res) {
var userId = req.session.user.id;

sails.log("Searching for foods for user: "+ userId);
Menu.find({ where: { userId: { equals: userId }}}).exec(function(err, records) {
    if(records && records.length == 0) {
      sails.log("No foods found for 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 = [];
          for (i = 0; i < 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 is the 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 now this line:

var foodCategories = records.food_category; 

      

creates undefined value in my Sails log when the menu / index view is loaded. Can anyone point me in the right direction?

+3


source to share


2 answers


The variable in your records is most likely an array of objects.

Move the code inside the for loop and use it like



records[i].food_category

      

you don't need var array ... In for loop use .lenght notations instead

+1


source


Many thanks to Molda for gently nudging you in the right direction!

Here is the Sails controller code that ended up for me:



var MenuController = module.exports = {

manager: function(req, res) {
  var userId = req.session.user.id;
  sails.log("Searching for foods for user: "+ userId);
  Menu.find({ where: { userId: { equals: userId }}}).exec(function(err, records) {
   if(records && records.length == 0) {
    sails.log("No foods found for user: " + userId);
    sails.log(err);
    return res.view("menu/index");
   } else {
    var foodCatArray = [];
      for (i = 0; i < records.length; i++) {
        var foodCategories = records[i].food_category;
        foodCatArray.push(foodCategories);
      }
    var uniqueCats = foodCatArray.filter(function (item, i, ar) {
      return ar.indexOf(item) === i;
    });
    uniqueCats.sort();
    sails.log(uniqueCats);
    sails.log("Returning " + records.length + " foods found for user: " + userId);
    return res.view("menu/index",{ foods: records, foodCats:uniqueCats });
  }
 });
},

      

This code skips every food_category line on every food item in the custom menu entries, adds it to an array, and then sorts the array, eliminating duplicates, and sorting the array alphabetically. In my opinion, I can use "foodCats" to grab this array and do whatever I need.

+1


source







All Articles