[solved] Inserting null values to meteor mongo


#1

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));
    });
  }

#2

Can you please edit your post and wrap your code in lines with triple backticks:

```
your
code
here
```

#3

Done, thanks for the suggestion


#4

Is your external application setting the Content-Type header to application/x-www-form-urlencoded when it makes the POST request?


#5

thanks a lot!!! for the solution. I really appreciate it :slight_smile:


#6

Madguy02 can you share what you did to fix this problem?


#7

Can you share what you did to fix this?


#8

i changed the header from application/json to application/x-www-form-urlencoded. This fixed my problem. Thanks to @robfallows