whiteboards / converse

Personal social expirement
MIT License
0 stars 3 forks source link

[user.js] Schema not registered #26

Closed NathanBland closed 8 years ago

NathanBland commented 8 years ago

We have a race condition as an issue here with this following function and I'm not sure how to solve it.

User.methods.addFriend = function (email, callback) {
  var query = Friends.find({
    email: email
  })
  var getUser = this.model('User').find({
    email: email
  })
  query.where('added').equals(true).sort('-id').exec(function (err, frinedReq) {
    if (err) {
      throw err
    }
    if (frinedReq.length < 1) {
      console.log('[user.js] No existing friend request')
      getUser.exec(function (err, friend) {
        if (err) {
          throw err
        }
        return Notification.create({
          user_id: friend._id,
          type: 'request',
          title: 'New Friend Request',
          content: {
            created_by: this._id,
            message: this.displayName + ' added you as a friend!'
          }
        }, callback)
      })
    } else {
      console.log('[user.js] Friend request already exists:', frinedReq)
      return callback
    }
  })
}

The issue occurs when we try to use the getUser query. One of two problems presents itself, either mongoose complains that the schema isn't yet registered, or that the User object doesn't have a find method. I believe I'm using the recommend method as described here so I'm not quite sure what to do about this, other than creating an abstraction layer that goes between the model and the routes file, but I'm not sure I like that idea either.

crodeheaver commented 8 years ago

What process are you going through to get this error? I've been trying to replicate it, but I haven't had any luck.

NathanBland commented 8 years ago

Ooops. This bug is actually fixed. My bad. The issue was this.model('User')should have had user being lowercase. There was no race condition like I thought.