1

Attempting to post a string from client to mongoDB in Sails

val is a string posted in real time from the client to the server database

POST Form( is html the right choice here? )

<!DOCTYPE html>
<html>
<body>
    <form action="post" method="post">
        <p>
            <label for="val">'Enter val'</label>
            <input type="text" name="val" autofocus />
            <input type="submit" />
        </p>
    </form>
</body>

`

routes.js (?)

'/post': {controller: "PostController", action: "post"}

Post.js

module.exports = {

attributes: { val: { type: 'string', required: true, unique: true } } }

PostController.js (?)

module.exports = {
var bodyParser = require('body-parser');
var app = express();
  app.post : function (req, res) {
  var val = req.body.val;   
  return res.send ('val': val);
  todo: 'Now how do I start loading into mongoDB ?';
}}

app.js

?

KEY QUESTION

How does val goes from the client to the database, that is :

what is the right sequence of events between component files in Sails ?

1 Answer 1

4

You need to go through and read documentation learn how sails works.

Try https://Sailsjs.org

and / or

http://irlnathan.github.io/sailscasts/blog/2015/01/01/lets-start-a-new-adventure/

For starters your controller is not written correctly

module.exports = {
  post : function (req, res) {
         var val = req.param('val');   
         return res.send ({'val': val});
     }
}

This should help a little, but your code was written in a way that says that your very unfamiliar with how sails works in general. You should read the docs or watch those videos linked above to get you started.

Sign up to request clarification or add additional context in comments.

4 Comments

we all were at once :). Those videos are new this year, they are a good resource!
Thank you for fixing the controller
req.param ('val') VS req.body ('val') ?
if using req.body then it would be req.body.val , req.param('val') is a catch all for query, body and parameters. So either way. req.param('val') allows you to also test with a query string, but if you know its a body param then just use body as it would be (mildly) more performant

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.