0

I am trying to take the ID of a found user and append it to an array of "Participants" in my mongo schema. Whenever I try to do this, it never actually adds to the array in the schema.

My schema:

// Group Schema
var GroupSchema = new Schema({
    name: String,
    subject: String,
    owner: { type: Schema.Types.ObjectId,
                    ref: 'User' }, 
    participants: [{ type: Schema.Types.ObjectId,
                    ref: 'User' }],

});

I have a function that finds users from usernames, and I want the found user to be added to the "Participants" array in the schema by their ID. This is what I have been trying:

...
if(errors) {
        res.render('creategroup', {
            errors:errors
        });
    }
    else {
        var addUser = req.body.addUser;

        User.getUserByUsername(addUser, function(err, user){
            if(err) throw err;
            if(!user){
                console.log("There's no user with that username");
            }
            else{
                res.redirect('/users/dashboard');
                console.log("A user was found!");
                var id = user.id;

                array.push(id);


            }
        });

        var name = req.body.groupname;
        var subject = req.body.groupsubject;
        var owner = req.user.id;
        var array = [];

        var newGroup = new Group({
            name: name,
            subject: subject, 
            owner: owner,
            participants: array,
        });

What should I be doing differently to add the user's ID to the participants array of the schema?

jblew
  • 274
  • 1
  • 8
  • 21
  • *"I have a function that finds users from usernames..."* Well presumably you mean that your UI is at some point representing the the "User" object from which you want to find the `_id` of which you want to "push" to the array in the particular group. Much in the way that your "Group" should be identified in the UI by it's own `_id` value, then so **"should"** your UI be aware of the `_id` of the current "User". This is a point of design which avoids unnecessary calls to do something like *"lookup by username"*. Because then it's just a point of performing an `.update()` with `$push` – Neil Lunn Jun 06 '17 at 01:17
  • So there are several problems with your logic here. The most notable is of course that your `new Group()` is "outside" of the callback from the function which is finding the "user". All such logic should be "inside" the callback. But the real note here is that you *"should"* be doing this in a completely different way, which does not require you to *"find the User"* at all. – Neil Lunn Jun 06 '17 at 01:20
  • Yeah, I have IDs for both my Groups and Users. But I don't know how to take the IDs of the Users and put them into the array of each group's participants. – jblew Jun 06 '17 at 01:22
  • Okay, the logic may not be the best, but that's not what I asked for help with. – jblew Jun 06 '17 at 01:26
  • Please read. *"your `new Group()` is "outside" of the callback"*. Which means the **async** function that **gets** a User **does not return** before the code that is presently trying to add the Group with the array. Take a breath and try to understand what was just said. – Neil Lunn Jun 06 '17 at 01:30
  • Thank you, that did the trick. I didn't mean to sound offensive, I just didn't realize that there was a problem with the logic. I thought I was misunderstanding how to push to an array. Thanks again! – jblew Jun 06 '17 at 01:38

0 Answers0