[Vuejs]-Accessing object properties in Vue after loading in Sails app

0๐Ÿ‘

โœ…

You should use findOne

// api/controllers/recipes/view-single-recipe.js

module.exports = {
  friendlyName: 'View single recipe',
  description: 'Display "Single recipe" page.',
  exits: {
    success: {
      viewTemplatePath: 'pages/recipes/single-recipe'
    }
  },

  fn: async function (inputs, exits) {
    const recipe = await Recipe.findOne({
      id: this.req.params.id
    }).populate('ingredients')

    return exits.success({ recipe: recipe });
  }
};

Also note that inputs variable in fn are not being used.
And there need to be some handler if record is not exists.

0๐Ÿ‘

The answer is that when using find() query, the results returned are an array. So if there is only one result, it needs to be accessed at the first result in the array [0]

// api/controllers/recipes/view-single-recipe.js

module.exports = {

// ...

  fn: async function (inputs, exits) {
    const recipe =  await Recipe.find({id: this.req.params.id}).populate('ingredients')
    console.log(recipe) //logs the data correctly
    return exits.success({
      recipe: recipe[0]
    });

  }


};

Leave a comment