Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getFriends issue #5

Open
rossmartin opened this issue Feb 4, 2015 · 4 comments
Open

getFriends issue #5

rossmartin opened this issue Feb 4, 2015 · 4 comments

Comments

@rossmartin
Copy link

I haven't had time to investigate what is causing this issue for me but when calling the instance method getFriends on a document of my user model I am getting the following -

/Users/rossmartin/git/chattur-server/node_modules/mongoose-friends/lib/plugin.js:328
            friendship.friend = res.friends[i];
                              ^
TypeError: Cannot set property 'friend' of undefined
    at /Users/rossmartin/git/chattur-server/node_modules/mongoose-friends/lib/plugin.js:328:31
    at /Users/rossmartin/git/chattur-server/node_modules/mongoose-friends/node_modules/async/lib/async.js:533:17
    at /Users/rossmartin/git/chattur-server/node_modules/mongoose-friends/node_modules/async/lib/async.js:119:25
    at /Users/rossmartin/git/chattur-server/node_modules/mongoose-friends/node_modules/async/lib/async.js:24:16
    at /Users/rossmartin/git/chattur-server/node_modules/mongoose-friends/node_modules/async/lib/async.js:530:21
    at Promise.<anonymous> (/Users/rossmartin/git/chattur-server/node_modules/mongoose-friends/lib/plugin.js:316:15)
    at Promise.<anonymous> (/Users/rossmartin/git/chattur-server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
    at Promise.emit (events.js:95:17)
    at Promise.emit (/Users/rossmartin/git/chattur-server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
    at Promise.fulfill (/Users/rossmartin/git/chattur-server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)

My temporary fix is to continue the loop in the situation that res.locals[res.friends[i]._id], friendship, is undefined -

below starts at 325 in plugin.js

          // wrap the results with the status
          for (var i=0, friendship; i<res.friends.length; i++) {
            friendship = res.locals[res.friends[i]._id];
            if (!friendship) continue; // this is the quick fix to my issue
            friendship.friend = res.friends[i];
            res.friends[i] = friendship;
          }

Any ideas what could be causing this? It is strange because it only occurs on a few documents of my user model and I can't pinpoint why either.

This issue is occurring in your latest version published to NPM and also with the latest commit that removed the dependency on underscore.js.

Thanks again for the awesome plugin.

@rossmartin
Copy link
Author

Providing the friends array for the troublesome user document might be of use, here is what it contains -

[
    {
        "_id" : ObjectId("534b8e5aaa5e7afc1b23e69b"),
        "status" : "accepted",
        "added" : ISODate("2015-01-28T03:42:17.348Z")
    },
    {
        "_id" : ObjectId("54544cb2164ec1df2325dd99"),
        "status" : "pending",
        "added" : ISODate("2015-01-29T19:17:44.912Z")
    },
    {
        "_id" : ObjectId("54c95b242ab6bc32636ad7b3"),
        "status" : "pending",
        "added" : ISODate("2015-01-30T03:27:39.021Z")
    },
    {
        "_id" : ObjectId("54c581a6ed542a6c4a9f7ffd"),
        "status" : "pending",
        "added" : ISODate("2015-02-02T18:19:41.145Z")
    }
]

@SnidelyWhiplash
Copy link

Ok so I was having a similar issue. Not sure if this fits your case, but here's what was happening for us:

We have an onboarding process during registration to help users find/add friends that are already on the site. As part of the onboarding, we're re-saving the user document to track their progress. The problem is that when re-saving on each step, it was setting the friends field back to an empty array.

So even though the user may have sent friend requests out on step 1, once they hit step 2 their account was re-saved with the empty array, leading to no records of sent requests on the current user, but still keeping the pending entries for the requested users, which will obviously cause issues.

Ultimately it came down to the model instantiation in our signup route. Psuedo-code:

app.post('/signup', function (req, res, next) {
  var user = new User(values); // Here, user.friends is set to an empty array! So our client model received it as such, and then when re-saving from the client it was setting back to that empty array!
  user.save(function (err, user) {
    if (err) return next(err);
    res.json(user);
  });
});

The solution is just to set user.friends = undefined just before the save, to ensure Mongoose won't return it at all after saving/sending to the client.

Hope that makes sense, and maybe helps you out.

@alivanov
Copy link

alivanov commented Aug 7, 2015

I've just faced the same problem. Updated mongoose module to 4.1.1 and it's gone

@alejandromdz
Copy link

Make sure your mongoose schema has the _id property with the right type:

_id:{type:mongoose.Schema.Types.ObjectId,required:true}

this worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants