Friday, 26 May 2017

Retrieve JSON values from ejs

I am new to node and ejs, I have below code, and need to know how to get the values from EJs file to post it to mongoDB

EJS file

<form>
    <p>
        <label for="username"> Username </label>
        <input type="text" for="username"/>
    </p>
    <p>
        <label for="password"> Password </label>
        <input type="password" for="password" />
    </p>
    <p>
        <label for="email"> Email </label>
        <input type="text" for="email"/>
    </p>
    <button type="submit">Log In</button> 
</form>

JS file

$('form').on('submit', function(){

      var item = $('form input');
      var user = [{username:item.val()},{password:item.val()},{email:item.val()}];

      console.log(user);

      $.ajax({
        type: 'POST',
        url: '/register',
        data: user,
        success: function(data){
          //do something with the data via front-end framework
            console.log(data);
          location.reload();
        }
      });

      return false;

  });

As of now when I insert to mongo DB I get a new uuid but the values are going in as "0"

please aasist



via Saurabh Jhunjhunwala

No comments:

Post a Comment