Question

I'm starting my adventure with NodeJS. I've choose express web framework with mongoose database. As part of my front-end I'm using AngularJS.

First the front end, basic form args passing to /api/user.

$scope.form = {};
  $scope.submitUser = function () {
    console.log($scope.form);
    $http.post('/api/user', $scope.form).
      success(function(data) {})
      .error(function(data) {});
  };

$scope.form equals to:

Object {name: "foo", surname: "bar", email: "foobar@bar.foo"} 

Then we got the back-end, where I start with default user schema:

var userSchema = new mongoose.Schema({
  name : String,
  surname : String,
  email : String
});

var User = mongoose.model('User', userSchema);

And the api post handler:

app.post('/api/user', function (req, res) {
  User.create({
    name : req.name,
    surname : req.surname,
    done : false
    }, function(err, user) {
      if (err)
        res.send(err);
      res.json(user);
    });
});

And the result of that instead of object is just id with __v:

__v: 0
_id: "536e218351b1182d0f000001"

PS. This one is magic, I have completly no idea why this is happening: Those records above __v and _id, are not being save anywhere (show collections, and find() on each of those results in null). But when I run

User.find(function(err, users) {
  if (err)
    res.send(err)
  res.json(users);

I get few records (previous tries of passing this form) with it's __v and _id.

Connection looks like this:

var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost:1/test');

Thanks for any tips, explanation to that. Sorry for my lack of knowledge but I really tried.

Have a nice day! ;)

Was it helpful?

Solution

Assuming that you send the json object in the body (payload) of POST from client, The server should access the value as req.body.name, not req.name

app.post('/api/user', function (req, res) {
    User.create({
        name : req.body.name,
        surname : req.body.surname,
        done : false
    }, function(err, user) {
      if (err)
        res.send(err);
      res.json(user);
    });
});

Make sure you pass body-parser middleware to express:

var express = require('express')
var app = express()
app.use(express.bodyParser());  // for express 3.x

or for express 4.x

var express    = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser());  // for express 4.x

OTHER TIPS

In my case it does not work. Why?

I am getting the value from req.body.name

If I log it console.log(req.body.name); I get the value on my console.

POST: 
{ name: 'typing any name', status: null }
typing any name

So the workflow between my frontend (angular.js), the form and the backend (node.js, express, mongoose) seems to work. Now I POST the value, but I get an empty object in my mongoDB.

{"_id":"543a50a974de6e2606bd8478","__v":0}

app.post('/api/offers', function (req, res){
var offer;
console.log("POST: ");
console.log(req.body);
console.log(req.body.name);
offer = new OfferModel({
 name: req.body.name,
 value: req.body.value,
 title: req.body.title,
 content: req.body.content,
});
offer.save(function (err) {
 if (!err) {
    return console.log("created offer" + req.body.name);
  } else {
    return console.log(err);
  }
});
 return res.send(offer);
});

And here is the model:

var offerSchema = mongoose.Schema({

    offer            : {
            name        : String,
            value       : String,
            title       : String,
            content     : String,
            image       : String,
            start       : String,
            end         : String,
            targets     : String,
            beacons     : String,
            published   : String
    }
 });
 var OfferModel = mongoose.model('Offer', offerSchema);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top