Skip to content

Cursor values get emptied when passed to Counts.publish() #58

@R3Shift

Description

@R3Shift

I'm not sure why, but when I save my search cursor to a variable (so that I can use it twice) and pass it into Counts.publish() and then use it again to return the values to the client, the cursor sent to the client has _id fields only. As you can see in the code below, the search specifies which fields are to be returned to the client, and this works just fine if the Cursor.publish() line is commented out. But with that line active the fields{} parameter seems to be overridden.

I did find a workaround (below in case it benefits others) but the workaround is a little less elegant and I wanted to ask if I was doing something wrong.

Is this the best way to do it?

    Meteor.publish("search", function (searchObj, limit, skip) {

        //build a search object that looks like {lastName: "Smith", firstName: "Jim"} (but with regex to fix case sensitivity)
        searchParameters = Meteor.call("buildSearchParameters", searchObj);

        if(!limit){var limit = 30} //in case the client code forgot to set one, or to prevent malicious overload
        if(!skip){var skip = 0}

        cursor = Contacts.find(
            searchParameters   //object built by buildSearchParameters method
            , 
            {
                limit: limit,
                skip: skip,
                fields: 
                {
                    lastName: 1, 
                    firstName: 1, 
                    middleName: 1,
                    streetNumber: 1,
                    streetName: 1,
                    city: 1,
                    zipCode: 1 
                }
            }
        ); 

        //use tmeasday:publish-counts package to publish the total count from this cursor
        //{noReady: true} allows the cursor to determine the limit rather than this counter (see docs)
        Counts.publish(this, 'totalSearchResultsCounter', cursor);
        return cursor;

Workaround: Instead of using cursor in the Counts.publish() function, I just do a new Contacts.find and bring down the same searchParameters variable that I was already doing above. This method requires that I save all of my search parameters into a separate object to avoid duplication (DRY).

                ...
        Counts.publish(this, 'totalSearchResultsCounter', Contacts.find(searchParameters);
        return cursor;

Thanks in advance, and thanks for this handy package !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions