balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Methods in REST Response #4446

Open tbiancoar opened 6 years ago

tbiancoar commented 6 years ago

Sails version: v1.0 Node version: v8.9.1 NPM version: v6.1.0 DB adapter name: sails-mongo DB adapter version: don't know Operating system: macOS

Hi, how are u?

I need to run a model method and add in response from Blueprint Actions!

For example, i have the following model:

module.exports = {

attributes: {

//  ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦  ╦╔═╗╔═╗
//  ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
//  ╩  ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝
tournamentStage: { type: 'number' },
matchTime: {
  type: 'ref',
  columnType: 'datetime',
  required: true
},
//percentRealMeat: { type: 'number', defaultsTo: 20, columnType: 'FLOAT' },

//  ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
//  ║╣ ║║║╠╩╗║╣  ║║╚═╗
//  ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝

//  ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
//  ╠═╣╚═╗╚═╗║ ║║  ║╠═╣ ║ ║║ ║║║║╚═╗
//  ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
// Add a reference to Teams
/*players: {
  collection: 'team',
  via: 'matchs'
},*/
localTeam: {
  model: 'team',
},
visitorTeam: {
  model: 'team',
},

localScorePrediction: function () {
  let myPrediction = 0

  /* CODE TO FIND MY PREDICTION */
  return myPrediction;
},
customToJSON: function () {
  console.log('hi, im hereee');
  return _.omit(this, [])
},

},

};

And when i request i don't see the "localScorePrediction" value:

[ { "createdAt": 1528080341248, "updatedAt": 1528080341248, "id": "5b14a7d503d4de23a69b3b82", "tournamentStage": 0, "matchTime": "2018-06-14:00:00.000Z", "localTeam": { "createdAt": 1528030218249, "updatedAt": 1528030218249, "id": "5b13e40a13cc3c0a94d170f7", "name": "Rusia", "img": "https://ssl.gstatic.com/onebox/media/sports/logos/5Y6kOqiOIv2C1sP9C_BWtA_48x48.png" }, "visitorTeam": { "createdAt": 1528030219436, "updatedAt": 1528030219436, "id": "5b13e40b13cc3c0a94d170f8", "name": "Arabia Saudita", "img": "https://ssl.gstatic.com/onebox/media/sports/logos/QoAJxO46fHid3_T-7nRZ0Q_48x48.png" } },

Any ideas?

Thanks!

sailsbot commented 6 years ago

Hi @tbiancoar! It looks like you may have removed some required elements from the initial comment template, without which I can't verify that this post meets our contribution guidelines. To re-open this issue, please copy the template from here, paste it at the beginning of your initial comment, and follow the instructions in the text. Then post a new comment (e.g. "ok, fixed!") so that I know to go back and check.

Sorry to be a hassle, but following these instructions ensures that we can help you in the best way possible and keep the Sails project running smoothly.

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact inquiries@sailsjs.com

ptdede commented 6 years ago

@tbiancoar If i'm correctly understand your question, you want to append some value to the result when you call Model action right? for example:

when you call Team.find({}) you expect manipulate the data and append your localScorePrediction to the result but you don't want to save it to database right?

for this you can use customToJSON method to append your localScorePrediction calculation. ex:

  /**
   * Omit password from returning User JSON.
   * do you want other user see their password?! 
   * Even tho its encrypted, it's pretty easy to decrypt.
   * This also demo append new things to User Object
   */
  customToJSON: function () {
    // ... do your calculation blablabla..

    const user = _.omit(this, ['password']);
    return _.assign(user, {localScorePrediction: 'your calculation result is here'}); 
    // _.assign is from lodash to append new value to an object.
  },

If you want to save localScorePrediction to database upon creation or update, you can use Model callback for this. see Docs

OR!

you can always do your calculation after getting Team Model inside your controller. it's pretty standard use case.