I am developing an app where I am using Node.js and MongoDB in the backend. The scenario is: The user fills all the details and post it to the server. The data is stored in MongoDB database with one ObjectID. Now I want to send that ObjectID as response to the user.
The code is given below:
router.route('/user')
.post(function(req, res) {
var user = new User(); // create a new instance of the User model
user.name = req.body.name; // set the user name (comes from the request)
user.email = req.body.email; // set the user email (comes from the
// request)
user.age = req.body.age; // set the user age(comes
user.save(function(err) {
if (err) {
res.send(err);
}
res.json({
message: 'User Created!',
});
});
The User Schema is given below:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
email: String,
name: String,
age: String,
});
module.exports = mongoose.model('User', UserSchema);
How will I send that ObjectID as response. Please tell me how it can be achieved
Thanks
function(err, data).. the data parameter will have the id of the user saved