Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6209439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:59:08+00:00 2026-05-24T05:59:08+00:00

I’m trying to save a document in my collection and if the save is

  • 0

I’m trying to save a document in my collection and if the save is successful, return the _id of this same document. The problem is I get an undefined value to my _id in both case, either the created model from mongoose or from the callback return. Basically, my only way of getting the _id would be to search the document by one of its properties, and then get the value. This approach isnt what I want, knowing what im currently trying to do should work.

var createTrophy = new Trophy({
    name            : post.name,
    accessCode      : post.password,
    description     : post.description,
    members         : [id]  
  });

  Trophy.findOne({name:post.name}, function(err, trophy) {
    if(err){
      console.log('Mongoose: Error: ' + err);
      res.send('Error db query -> ' + err);
    }
    else if(trophy){
      console.log('Trophy ' + trophy.name + ' already existant');
      res.send('Trophy ' + trophy.name + ' already existant');
    }else{  
    createTrophy.save(function(err, doc){
      var uid = createTrophy._id;
      if (err) { 
        console.log('Error in trophy saving:' + err); 
        res.send('Error in trophy saving:' + err);
      }else{ 
        User.findOne({_id:post.id}, function(err, user) {
          if(err){
            console.log('Mongoose: Error: ' + err);
            res.send('Error db query -> ' + err);
          }
          else if(user){
            console.log(doc._id + ' ' + uid);
            user.trophyLink = doc._id;
            res.send(user);
            //user.save(function(err){
            //   if(err){res.send('Couldnt update trophy of profile');} 
            //});
          }
          else{
            console.log('User id Inexistant'); 
            res.send('User id Inexistant');
          }
        });
      }
    });
    }  
  });
});

The Schema

 var Trophy = new Schema({
        _id             : ObjectId,
        name            : String,
        accessCode      : String,
        description     : String,
        //reference to User ID
        members         : [Number],
        comments        :[Comment]
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:59:09+00:00Added an answer on May 24, 2026 at 5:59 am

    you don’t have to supply _id in your Schema, it’ll be generated automatically. and if you want the name to be unique you can also configure this in the Schema. if members are supposed to be “real” user _ids, than try sth like [ObjectId].

    var TrophySchema = new Schema({
      name: {type:String, required:true, unique:true},
      accessCode: String,
      description: String,
      //reference to User ID
      members: [ObjectId],
      comments: [Comment]
    });
    

    and i don’t know if this works

    var trophy = new Trophy({...data...});
    

    like you did it, i always do it like this:

    var trophy = new Trophy();
    trophy.name = "my name";
    // ...
    

    and the _id should be set as soon as you create the object (http://stackoverflow.com/questions/6074245/node-mongoose-get-last-inserted-id).

    so just do it this way:

    trophy.save(function (err) {
      if (err) {
        if (err.toString().indexOf('duplicate key error index') !== -1) {
          // check for duplicate name error ...
        }
        else {
          // other errors
        }
        res.send('Error in trophy saving:' + err);
      }
      else {
        User.findOne({_id:post.id}, function(err2, user) {
          if (err2) {/* ... */}
          else if (user) {
            user.trophyLink = trophy._id;
            res.send(user);
          }
        }
      }
    });
    

    important is, that save doesn’t return the trophy you have to use the one you created yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.