Hi,
I have been working on a CRUD API, on meteor but my api posts null value to the fields, how do i solve it?
if(Meteor.isServer) {
Meteor.startup(function () {
User = new Meteor.Collection('enroll', {Type: "string"}) ;
});
Router.route('/enroll',{where: 'server'})
.get(function(){
var response = User.find().fetch();
this.response.setHeader('Content-Type','application/json');
this.response.end(JSON.stringify(response));
})
.post(function(){
var response;
// before insertion, we can put A condition for request.body === undefined, before insertion
User.insert({
UserName : this.request.body.userName,
UserCourse : this.request.body.userCourse
});
response = {
"error" : false,
"message" : "Course added."
}
this.response.setHeader('Content-Type','application/json');
this.response.end(JSON.stringify(response));
});
Router.route('/enroll/:id',{where: 'server'})
.get(function(){
var response;
if(this.params.id !== undefined) {
var data = User.find({_id : this.params.id}).fetch();
if(data.length > 0) {
response = data
} else {
response = {
"error" : true,
"message" : "Course not found."
}
}
}
this.response.setHeader('Content-Type','application/json');
this.response.end(JSON.stringify(response));
})
.put(function(){
var response;
if(this.params.id !== undefined) {
var data = User.find({_id : this.params.id}).fetch();
if(data.length > 0) {
if(User.update({_id : data[0]._id},{$set : {UserName : this.request.body.userName,UserPassword : this.request.body.userPassword}}) === 1) {
response = {
"error" : false,
"message" : "Course information updated."
}
} else {
response = {
"error" : true,
"message" : "Course information not updated."
}
}
} else {
response = {
"error" : true,
"message" : "User not found."
}
}
}
this.response.setHeader('Content-Type','application/json');
this.response.end(JSON.stringify(response));
})
.delete(function(){
var response;
if(this.params.id !== undefined) {
var data = User.find({_id : this.params.id}).fetch();
if(data.length > 0) {
if(User.remove(data[0]._id) === 1) {
response = {
"error" : false,
"message" : "Course deleted."
}
} else {
response = {
"error" : true,
"message" : "Course not deleted."
}
}
} else {
response = {
"error" : true,
"message" : "Course not found."
}
}
}
this.response.setHeader('Content-Type','application/json');
this.response.end(JSON.stringify(response));
});
}