Hello, I am new to Node and so far having a blast. I'm writing a simple API 
but am having trouble getting data from the json payload in the body to 
create a new customer. Any help would be appreciated.

*Sample Payload passed in using curl (createCustomer.json):*
{
"firstName": "Mike",
    "lastName": "Ditka",
    "email": "[email protected]",
    "username": "mikeditka",
    "password": "password",
    "address": {
      "street": "85 Bears Ave.",
      "city": "Chicago",
      "state": "IL",
      "zip": 12345
    }
}

*using*: curl -is -X POST -d "@createCustomer.json" 
http://localhost:3000/customers


*customer schema:*
var customerSchema = new Schema({     
    firstName       : String, //{ type: String, required: true, trim: true 
},
    lastName       : String, //{ type: String, required: true, trim: true },
    username        : String, //{ type: String, required: true, trim: true 
},
    password        : String, //{ type: String, required: true, trim: true 
},
  email : String, //{ type: String, required: true, trim: true },
  phone : String,
    image : String,
    created : {type: Date, default: Date.now},
    address: {
        street : String,
    city : String,
    state : String,
    zip : Number
  }
});

*customers.js*
exports.customer = function(req, res) {
  console.log(req.body);
    
var customer = new Customer({
firstName: req.body.firstName,
lastName: req.body.lastName,
username: req.body.username,
password: req.body.password,
email: req.body.email
/*customer.phone = req.body.phone;
customer.image = req.body.image;
customer.address.street = req.body.street;
customer.address.city = req.body.city;
customer.address.state = req.body.state;
customer.address.zip = req.body.zip;*/
});
 customer.save(function (err) {
    if (!err) {
      return console.log("created");
    } else {
      return console.log(err);
    }
  });
  return res.send(customer);
}

*server.js*
// require restify and bodyParser to read Backbone.js syncs
var restify = require('restify');  
var server = restify.createServer();

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

server.pre(function(req, res, next) {
  req.headers.accept = 'application/json';
  return next();
});

// Set up our routes and start the server
server.get('/customers', customers.list);
server.post('/customers', customers.customer);

Can anyone make sense of what I posted to understand why when I POST using 
that curl command, it creates a new document, but it only contains the ids 
created by MongoDB - no customer data?


-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to