Custom JSON and Waterline User Model
Fullstack Social iOS NodeJS REST
Some developers might not like it, but I think of the best features that comes with Sails is the build in ORM tool called Waterline. This database tool allows us to easily fetch and create objects from our databases. In today's lesson, I'll start off customizing our server very simply by adding a custom route that lists out all the current users. Along the way, you'll be able to see that the print out in JSON is a little too overwhelming. Because of this, I'll show you how to omit data as well as customizing our User object JSON.

Comments (8)
Maxnelson997
4 years ago
What the.. You don't even need to require the User model into the file..
Brian Voong
4 years ago
Mohammed Salem
4 years ago
Do i have to be logged in to get the json data?
Brian Voong
4 years ago
objcxcode
4 years ago
Hello, After define route 'GET /listusers': 'user/listusers' at route file and create new user folder and listusers.js file under controller. I got the following message and the page still shows 404: ``` warn: --------------------------------------------------------------------------- warn: Files in the `controllers` directory may be traditional controllers or action files. Traditional controllers are dictionaries of actions, with pascal-cased filenames ending in "Controller" (e.g. MyGreatController.js). Action files are kebab-cased (e.g. do-stuff.js) and contain a single action. The following file was ignored for not meeting those criteria: warn: - user/listusers.js warn: ---------------------------------------------------------------------------- warn: Ignored attempt to bind route (/listusers) to unknown action :: user/listusers ``` The first one seems the naming issue and the second seems it's the main problem to cause 404. What could be the step I miss? Thanks
Brian Voong
4 years ago
pdefilippi
4 years ago
Few questions Bryan, In the User.js file we are creating a customToJSON function. Is there any type of functionality that lets us Command click / jump to definition like we can in Xcode? In the listusers.js file I am trying the same thing with User.find(). I am ultimately trying to make the connection between User.find() returning customToJSON. Unless I am missing something...
pdefilippi
4 years ago
I think I answered the second part of my question after looking at the two files again. These are two separate calls independent of each other? Still curious about the first part of my question above....
Brian Voong
4 years ago
julioest
3 years ago
Where are all the "full" set of JSON objects with all their attributes coming from?
Brian Voong
3 years ago
ddramond
3 years ago
Hey Brian, I have also recently followed this series and I didn't have a User.js file in the models folder. I don't think this is created by default anymore so I had to copy it from the downloadable project you provided. This might be what julioest is referring too.
ddramond
3 years ago
I've just tracked back and realised that you mentioned starting with an empty project is a good idea in Ep. 3, so I got ahead of myself and went with an empty project, even though you select Web App. My bad! This may also be what julioest has done? I could be wrong. Selecting an empty project will 1, not create the User.js file and 2, even if you copy it over, you aren't able to create an account to access that User.js file :/
Chauncey
3 years ago
hello,I want to purchase the courses, the video has subtitles? where watch video? It would be great if it was on Youtube!
AlfieLBTA
3 years ago
Is there a way (maybe will be used on a later chapter) to have several customJSON responses from the model depending of the controller that is asking for it ? , maybe one controller just needs the billing info and another controller just the email reseting data.
Brian Voong
3 years ago
Setwork44
3 years ago
I copied and pasted word for word the customJSON but its not 'lifting' when I save and I keep getting a red line on the attributes line underneath it..
Setwork44
3 years ago
even the so called silly way gives me this error: "TypeError: Cannot read property 'forEach' of undefined at Object.module.exports"
HELP & SUPPORT