Custom Field with ReactNative

Hello there, how can I subscribe to a custom user field ?

Here is the JSON format for Meteor.users collection in the DB

{
    "_id" : "pziqjwGCd2QnNWJjX",
    "createdAt" : ISODate("2017-12-21T22:06:41.930Z"),
    "emails" : [ 
        {
            "address" : "email@email.com",
            "verified" : false
        }
    ]
    "votes" : {
        "laws" : [ 
            {
                "ZyYZ4LDTaeWNMN9eE" : "oui"
            }
        ]
    }
}

PUBLICATION

Meteor.publish("UserVotes", () => {
if (!this.userId) return null;
  return Meteor.users.find(this.userId, { fields: [votes] });
});

SUBSCRIPTION

export default createContainer(params => {
  Meteor.subscribe("UserVotes");
  return Meteor.users.find(this.userId, {
    fields: {
      votes: 1
    }
  });
}, UserVotes);

It returns users.find is not an object, any idea?

Your publication is passing an array with a single element of undefined to the fields key. The fields key should be defined just like you have it in your createContainer code.

Also this article may be helpful. https://medium.com/differential/connecting-react-native-and-meteor-cccdb0df29e3

You will have to use this format in React Native.

Meteor.collection('users').find({ _id: params.userId });

Hi there, the problem is that you use this in arrow function, the publish function should be written like this:

Meteor.publish("UserVotes", function () {
  if (!this.userId) return null;
  return Meteor.users.find(this.userId, { fields: [votes] });
});
1 Like

Good catch on that, I didn’t even pay attention to the function type :slight_smile:

Alright , thank you, I will check that !