-2

I write an application in SailsJS and i have a problem with relations,

some code at the beginning:

User.js

module.exports = {
tableName: 'user',
attributes: {
    schedules: { collection: 'userworkstationschedule', via: 'user' },


}

UserWorkstationSchedule.js

 module.exports = {
 tableName: 'user_workstation_schedule',
 attributes: {
    user: { model: 'user', required: true },
   }
}

After run my code in Postaman, in JSON response i get:

{
...
  "user": 2,
...
}

I get only ID of my user, but i want to get a whole object of User model with his firstname, lastname, etc.

Ccould anyone help me?

henry123
  • 17
  • 2
  • You gotta be more elaborate with your question. You need to add the `user` table structure, the `user_workstation_schedule` structure, and you need to tell us how you're trying to create the tables. Are you using SailsJS to migrate these tables or are you simply using SailsJS to query them? Also, I prefer not using waterline and simply sticking to pure MySQL queries – TheLebDev Feb 28 '20 at 16:08

1 Answers1

0

I'm more accustomed to a sails 0.12, but I know there you can configure your app so that population happens automatically, but it is not the default - to do this, go into config/blueprints.js and set the populate option to true.

However, I recommend against this - population may not be something you need on every page / every api call. You can make your api calls with population requests built in, like so:

/api/userworkstationschedule/[workstationid]?populate=user

That should make the populate happen just for that api call.

arbuthnott
  • 3,819
  • 2
  • 8
  • 21
  • Hmm, too bad. But it looks like the url method still works: https://sailsjs.com/documentation/reference/blueprint-api/find-one In my opinion, that's better anyway. Getting in the habit of just blanket populating all records will lead to an app doing lots of unneccesary work. – arbuthnott Mar 03 '20 at 15:59