Save Posts with HTML Form
Fullstack Social iOS NodeJS REST
Now that you have a bit of understanding on creating objects using Waterline, let's move on and start saving post objects in our database. The easiest way to do this is to create an HTML form that allows us to submit objects to our server. Forms can pass data through "POST" requests on the request body, which we will access on the server side. Taking care of this step will allow us to display posts in HTML as well using EJS, a javascript templating engine.

Comments (8)
Christopher J. Roura
4 years ago
Hi, I keep getting this log message, "(node:8764) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated" Whenever I get this message all of the users somehow get wiped out, including the default user and it forces me to create new users. I assume I am doing something wrong but am unsure of where to check.
Brian Voong
4 years ago
Christopher J. Roura
4 years ago
Ah gotcha, thank you!
ogarci10
4 years ago
Hello Brian, When I write the waterline function 'await Post.create({ text: postBody })' it gives me a parsing error, I have the create the model to be Post.js and followed everything on vid.
Brian Voong
4 years ago
ogarci10
4 years ago
create.js file - "module.exports = function(req, res) { const postBody = req.body.postBody sails.log.info("Post Created: " + postBody) const record = await Post.create({ text: postBody }).fetch() res.send(record) }" Post.js file - "module.exports = { attributes: { text: { type: 'string', required: true } }, };" for ';' i noticed you did not include this in the video, but in the User.js file it had it at the end, I am more of a C++/Java programmer so still getting used to some of the JS syntax.
Brian Voong
4 years ago
ogarci10
4 years ago
What do you mean by in front of my function? Like for example: module.exports = await function(req, res)...?
Brian Voong
4 years ago
ogarci10
4 years ago
haha yes it did fix it! Thank you!
Christopher J. Roura
4 years ago
Ah, this did the trick. I had the await but not the async keyword included
pdefilippi
4 years ago
Hey Bryan, I just want to confirm that we get .create() and .find() from Waterline or is that a JS thing?
Brian Voong
4 years ago
pdefilippi
4 years ago
awesome
Wiajayi
3 years ago
Hi Brian, Do you have any information or courses/lessons on how to implement the CSRF for production? I am currently stuck on the custom login & sign up page, it works when set to false but I want to understand how to set it to true and provide it with a token so it is ready for prod
Brian Voong
3 years ago
Wiajayi
3 years ago
Thanks Brian, Also for your content! helps me alot!
Brian Voong
3 years ago
rcao140
3 years ago
Hi Brian, I am confused about using <% text %>, why is it done this way? Is it because it's an ejs file?
Brian Voong
3 years ago
alejandro
3 years ago
Hey brian i have a question.. why in the models -> Post.js you dont need to use function(req, res) ?
arpethel
3 years ago
Brian! I'm having issues with Post not being defined. I have async and await for my function and Post module call, but the error won't go away. Any ideas? //***for create.js*** module.exports = async function(req, res) { const postBody = req.body.postBody; const record = await Post.create({ text: postBody }).fetch(); res.send(record); }; //***and for home.js*** module.exports = async function(req, res) { const allPosts = await Post.find(); res.view('pages/post/home', { allPosts }); };
Brian Voong
3 years ago
arpethel
3 years ago
Yep, Post.js is in there :)
arpethel
3 years ago
I'm getting this when I hover over Post: 'Post' is not defined.eslint(no-undef)
Brian Voong
3 years ago
arpethel
3 years ago
Sounds good, Brian. Thanks!!
gerkov77
3 years ago
Hello Brian, Great course as always! One problem occurred as I followed the tutorial. Requesting localhost:1337/post is returning 404 page not found error! I followed everything exactly in routes and in home.js , and hit option command save too. I have tried making a Post.js model too, in hope that it would work -but didn't. (/listusers endpoint is working though) ((Also there is a deprecation error telling "(node:6190) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated". Maybe this has to do something with my 404 error here? I think you answered this one below already)) Could you give me a hint here? Thanks I advance! Greg
gerkov77
3 years ago
I have found this comment in models.js : // /\ Using MongoDB? // || Replace `id` above with this instead: // // ``` // id: { type: 'string', columnName: '_id' }, // ``` // // Plus, don't forget to configure MongoDB as your default datastore: // https://sailsjs.com/docs/tutorials/using-mongo-db Should I go ahead and do that? (I am using Atlas ) Is this gonna solve my problem maybe?
gerkov77
3 years ago
OK! Seems that I wasn't as accurate as I thought! My /post route had 'post/home/' instead of 'post/home'. Easy to make a mistake with those strings!
Brian Voong
3 years ago
gerkov77
3 years ago
Cool, thanks :) !
HELP & SUPPORT