0

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

3
  • Please clarify one point - you're using Mongoose to talk to MongoDB? Can you provide the code for your User model? Commented Nov 26, 2014 at 14:50
  • 1
    change the callback function to function(err, data).. the data parameter will have the id of the user saved Commented Nov 26, 2014 at 14:51
  • @Prisoner: Yes I am using mongoose. Commented Nov 26, 2014 at 15:03

2 Answers 2

2

It seems like you're using an ODM such as Mongoose in addition to MongoDB. You'd have to check that ODM's documentation for what you want to do. But usually, once you have the record whose Id you want, you'd do something like:

user.save(function (err, data) {
  if(err) {
    //handle the error
  } else {
    res.send(200, data._id);
  }
});

Here, we're taking advantage of the fact that every Mongo record's ObjectID is stored as its _id property. If you're only using Mongo and not an ODM, you could also search for the record once it's saved and get the _id property that way.

collection.find(/* search criteria */, function (err, data) {
  //same as before
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @A. Duff...Let me do it...I am replying back to you ASAP
Getting null in response
According to the Mongoose documentation it should be passing in the saved data as the second parameter. Try printing out id instead of _id. Also try printing out the whole returned object to see what you're getting.
0

You need a second parameter in callback like this:

user.save(function(err, description){
  var descriptionId = description._id;

  res.send(descriptionId);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.