Anna F
Anna F

Reputation: 1683

Post Request from express to mongoDB

I am trying to post the information to the mongoDB with Express. All connections to DB are working fine, because get Request works

In my server.js I have defined the Schema

var todoSchema = new Schema({
    taskName: String,
    createdAt: Date,
    isDone: Boolean,
    prioraty: String
  }, {
    collection: 'tasks'
  });

var Model = mongoose.model('Model', todoSchema);

And then the app.post Request

app.post('/tasks', function(req, res) {
  var savedata = new Model({
    'taskName': req,
    'isDone': false,
    'createdAt': Date.now(),
    'prioraty': 'medium'
  }).save(function (err, result) {
    if (err) throw err;

    if (result) {
      res.json(result)
    }
  })
});

and this doesn't work at all...

In my frontend I call it by pressing the button, but nothing happens..

  postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
  }

How could I fix it? As mentioned, the GET request works fine, so I hope I am on right way...

So here is get Request for example

app.get('/tasks', (req, res) => {
  Model.find({
   // 'request': query
  }, function(err, result) {
    if (err) throw err;
    if (result) {
      res.json(result)
    } else {
      res.send(JSON.stringify({
        error : 'Error'
      }))
    }
     })
    });

Upvotes: 0

Views: 2296

Answers (2)

Sravan
Sravan

Reputation: 18647

First you need to recieve your response in frontend

In service:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}

If you are using angularjs(angular 1+)

You need to use then and catch to recieve the response

In controller:

this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})

If you are using angular(angular 2+)

You need to use subscribe as this.http returns observable

In Component:

this.service.postTask(task).subscribe( response => {
    console.log(response)
})

Second, as @andrew mentioned,

taskName you should send string since you have taskName schema as string

var todoSchema = new Schema({
    taskName: String,
})

Update:

req.body Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser

The following example shows how to use body-parsing middleware to populate req.body.

var app = require('express')();
var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

    app.post('/tasks', function(req, res) {
       console.log(req.body)
    });

Here is Documentation of body-parser

Upvotes: 1

Andrew Lobban
Andrew Lobban

Reputation: 2137

Looking at your POST, taskName is a string based on your Schema, but you are passing it the complete req object. It should be what ever the property in the req.body is like req.body.taskName if that is what you are passing in your request.

Upvotes: 2

Related Questions