How to check a whether a certain field in mongo document is true or false

I want to check whether the logged in user has a certain field true or false within their user document. My current rendition passes an object of information, but I just want a true or false.

This is the publish function I made on the server side. I wanted this to return true if the fields ‘profile.completeRegister’ were true

Meteor.publish('completeRegis', () => {
        if (this.userId) {
            
            return Meteor.users.find({ $and: [ { _id: this.userId }, {'profile.completeRegister': true}]}).count() !== 0
        }
    });

This is the function on the client side that I wanted to return the true or false dependant on the publish function.

function completeRegister(){
    return Meteor.subscribe('completeRegis');
}

I did a cosole.log and ill show you the results

function logValue(){
    var profileConfig = completeRegister();
//SOME OTHER CODE
    console.log(profileConfig);
}

This is what the console.log shows

{stop: ƒ, ready: ƒ, subscriptionId: "gB7dtHFSyerJfkr5L"}
ready
:
ƒ ready()
stop
:
ƒ stop()
subscriptionId
:
"gB7dtHFSyerJfkr5L"
__proto__
:
constructor
:
ƒ Object()
hasOwnProperty
:
ƒ hasOwnProperty()
isPrototypeOf
:
ƒ isPrototypeOf()
propertyIsEnumerable
:
ƒ propertyIsEnumerable()
toLocaleString
:
ƒ toLocaleString()
toString
:
ƒ toString()
valueOf
:
ƒ valueOf()
__defineGetter__
:
ƒ __defineGetter__()
__defineSetter__
:
ƒ __defineSetter__()
__lookupGetter__
:
ƒ __lookupGetter__()
__lookupSetter__
:
ƒ __lookupSetter__()
get __proto__
:
ƒ __proto__()
set __proto__
:
ƒ __proto__()

How should I change the code?

I find that the best way to do something like this is to ‘null’ publish the user account document like this, in a server loaded file:

Meteor.publish(null, function () {
	if (this.userId) {
		return Meteor.users.find(this.userId, {
			fields: {
				name: 1,
				isAdmin: 1,
				// ...etc...
			}
		});
	} else {
		this.ready();
	}
});

This will automatically publish the user document, I limit the result to only share certain fields (see fields). The document will be kept in sync as its a publication. No need to run a separate publication function.

Because the publication name is null it automatically publishes to the clients. You don’t need to call it from the client. I believe that this only works once in an app.

BTW, in the case of the current user (like it appears to be the case here), the profile document is automatically published.

1 Like