3

Hi i am a new bee to sails and trying to get a model api that finally gives output as follows

[ 
  { 
    "icon" : [ 
      {"name":"ico1", "ico_typ":"fb", "ico_content_URL":"someLocation"},
      {"name":"ico2", "ico_typ":"tw", "ico_content_URL":"someLocation"},
      {...}
      ]
      "createdAt":
      "updatedAt":
     
  } 
]

I thought i can achieve this by passing the icon attribute as an Array but the problem is it passes the whole Array as string when i load it in REST CLIENT also i could not use the validation for values inside Array like without ico_type and URL the data should not be loaded into database. So, any suggestion about use of the 'array' where i'm wrong is very appreciated, thanks so much! Sails_v0.11.0 MongoDB_3.0.1

2
  • what do you use to send back the response ? res.ok({}) ? Commented Apr 3, 2015 at 6:53
  • no i created an icon model and defined an attribute there {icon : { type : array, required:true}} Commented Apr 3, 2015 at 6:58

2 Answers 2

1

In your model define a method

toJSON: function () {
   var obj = this.toObject();
   //say your obj.icon returns something like `'[{"name":"ico1","ico_typ":"fb","ico_content_URL":"someLocation"},{"name":"ico2","ico_typ":"tw","ico_content_URL":"someLocation"}]'`
   obj.icon = JSON.parse(obj.icon)
   return obj;
},
Sign up to request clarification or add additional context in comments.

2 Comments

Yes That's Perfectly works but i also need to validate the contents inside the array like the array must be created only if it has the valid URL
thats another issue, You can add something like beforeCreate: function (values, next) { .... } And you can accept the answer, so that other can be benefited :)
1

I think the model WaterLine gave you is already an JSON format, all you need to do is to use the right way to response it.

res.json(model);

1 Comment

I get the result Json as i need but by aim is to use the waterline validations inside the json object. any suggestions to that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.