0

I have a collection called ChatRooms that has an array of chatIds, which are just the user Ids of who is in the chatroom. There are only two ids in the array at a time. I want to find the first element in this array and set it to the variable "messager" so that i can see if it belongs to the person currently logged in. if not, the message that was just sent will be added to a different collection called notifications, so that the other user can be notified of receiving a message. I keep getting syntax errors on the query, and I don't know why. Here is my block of code:

createMessageNotification = function(message) {
  var messager = ChatRooms.find( {}, { chatIds: { $slice: 0,1 } } );
  //console.log(messager);

  if(messager !== Meteor.userId()){
    Notifications.insert({

      message: message.value
    });
  }
}
pat
  • 127
  • 1
  • 1
  • 12

1 Answers1

1

$slice needs a scalar or an array parameter. docs Also you should be doing a findOne which returns an object instead of a find which returns a cursor.

Try either:

var messager = ChatRooms.findOne( {}, { chatIds: { $slice: 1 } } ).chatIds;

or

var messager = ChatRooms.findOne({}).chatIds[0];
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39