I have a User form and I am trying to do a POST. Earlier code with schemas work. I am fitting in AJAX just now and I dont have a clue why it is not working. There is no error.
Below is code snippet from users.html file
</div>
<div class="btn" role="group">
<button type="button" name="submit" id="Submit" class="btn btn-primary" onclick="onSubmit()">Submit</button>
</div>
<div class="btn" role="group">
<input class="btn btn-warning" type="reset" value="Reset">
</div>
</form>
<script type="text/javascript">
function onSubmit() {
console.log("Ajax Testing");
AJAX('POST /users', function (value, err, response) {
if (err) { throw err };
console.log(value);
console.log(response);
});
};
</script>
Here is the routing part from default.js file
exports.install = function () {
ROUTE('GET /', displayHome);
ROUTE('GET /{userId} *userSchema --> @query');
ROUTE('GET /homepage', displayHome);
ROUTE('POST /users *userSchema --> @insert');
Below is the schema for Insert operation
schema.setInsert(function ($) {
var userDB = DATABASE('users');
userObj = {
name: $.controller.body.name,
age: $.controller.body.age,
height: $.controller.body.height,
email: $.controller.body.email,
gender: $.controller.body.gender,
cities: $.controller.body.cities,
landline: $.controller.body.telephone,
about: $.controller.body.aboutMe
};
userDB.insertOne(userObj, function (err, result) {
if (err) throw err;
console.log("Created New user using Schema Definition" + result);
$.controller.view('homepage');
}.bind($.controller));
});
Can you please let me know what is the error?
You forgot to send a data:
BTW: the code is not good in your schema. You need to work with fields defined in the schema and then everything is much simpler:
Learn from examples: