0

So I have the following publish method that creates a Room with many parameters:

Meteor.publish('addRoom', function publishFunction(options, admin, adminKey, members, roomName, isOpen, finalChoice, timeStamp) {
    //add relevant info to collection
    Rooms.insert({
        optionsList: options,
        optionsArr: options.split(','),
        admin: admin,
        adminKey: adminKey,
        membersArr: members,
        roomName: roomName,
        isOpen: isOpen,
        finalChoice: "A choice will be made when the room is closed",
        timeStamp: timeStamp
    });
    return Rooms.find({}, {sort: {timeStamp: -1}});
});

This (in theory) should return a cursor to a document with all the information just passed in.

This is my subscribe:

var getRoom = Meteor.subscribe('addRoom', options, admin, adminKey, members, roomName, isOpen, finalChoice, timeStamp));
console.log(getRoom.adminKey.toString());

I know all those variables that are passed in are defined correctly, however that console.log(...) gives me an undefined error.

However, Rooms.find().fetch() in the chrome's console does show the document correctly.

How can I get the admin key of the document correctly?

EDIT:

I should also note that I have Rooms = new Meteor.Collection('rooms'); at the top of both my sever-side and client-side javascript files.

bafrick
  • 17
  • 5
  • 1
    You really shouldn't do an `.insert()` inside a publish function. That's going to be unpredictable. You should do that in `Meteor.startup()` or explicitly as a method call. – Michel Floyd Jun 15 '16 at 19:44
  • @MichelFloyd What would it look like as a method call, and will I be able to accomplish my initial goal that way? – bafrick Jun 15 '16 at 20:06
  • 1
    `Meteor.subscribe` returns a subscription object, not the actual collection. Your `getRoom` variable doesn't have an `adminKey` field. You can get them using `Rooms.findOne`. Please check out the Meteor tutorial about subscriptions: https://www.meteor.com/tutorials/blaze/collections – aedm Jun 15 '16 at 22:40

0 Answers0