Publications and Methods as REST API

Hello,

I’m trying to follow the Meteor Guide at Publications and Data Loading | Meteor Guide at chapter " Accessing a publication as a REST endpoint". I did

meteor add simple:rest

But when I try to issue the command ```
curl localhost:3000/users/login -H “Content-Type: application/json” --data ‘{“email”: “user@example.com”, “password”: “password”}’

Meteor answers with some HTML :

DOCTYPE html
html
head
link rel=“stylesheet” type=“text/css” class=“meteor-css” href="/merged-stylesheets.css?hash=6b1f9f6fb78291ae58da8ec4f36476931155453c"
title my-app /title

What am I missing ?

I think you are missing publications in your URL

The example code in the Meteor guide does this:

 curl localhost:3000/publications/lists.public

This will access a publication called lists.public

Hi,
I’m trying to access a private publication so before accessing it I need to get a bearer token.

I finally managed to get it with the request
curl http://customer01.localhost/users/login -k -L -H "Content-Type: application/json" --data '{"username": "name@email.com", "password": "PWD"}'
(the error I got before was due to some nginx config error)

The response gives me a token which I use in the actual publication query

curl https://customer01.localhost/publications/sessions.listAssignedOpenSessions -k -L -H "Authorization: Bearer XD2aAdw0iXFVgHY7vDXYP0h7KtsbgDaoFhTEUpIsmgg"

But then I get the error

Error: Meteor.userId can only be invoked in method calls or publications. at AccountsServer.userId (packages/accounts-base/accounts_server.js:124:13)

On server side the errors are

W20220523-10:19:14.914(2)? (STDERR) Error: Meteor.userId can only be invoked in method calls or publications.
W20220523-10:19:14.915(2)? (STDERR)     at AccountsServer.userId (packages/accounts-base/accounts_server.js:124:13)
W20220523-10:19:14.915(2)? (STDERR)     at AccountsServer.user (packages/accounts-base/accounts_common.js:165:25)
W20220523-10:19:14.915(2)? (STDERR)     at Object.Meteor.user (packages/accounts-base/accounts_common.js:415:35)
W20220523-10:19:14.915(2)? (STDERR)     at getOrCreateTenant (imports/model/Tenant.ts:18:25)
W20220523-10:19:14.916(2)? (STDERR)     at HttpSubscription.<anonymous> (imports/api/sessions/server/publications.ts:10:20)
W20220523-10:19:14.916(2)? (STDERR)     at packages/simple_rest.js:241:33
W20220523-10:19:14.916(2)? (STDERR)     at packages/simple_json-routes.js:100:9

My publication called “sessions.listAssignedOpenSessions” is implemented in imports/api/sessions/server/publications.ts so it seems it finds it fine, but why can’t it access AccountsServer.user ? as this is the point of making the authenticated call ?

I’m using Meteor 2.7.2 with simple:rest 1.2.1

Any idea ?

Would you be able to provide the code snippet?

Hello,
Yes, I can provide some code. Publication is

Meteor.publish('sessions.listAssignedOpenSessions', function (subDomain) {
    const tenant = getOrCreateTenant(subDomain)
    checkPermission(Permissions.listSessions)
    if (!this.userId) return this.ready();
    return SessionsCollection.find({ tenantId: tenant._id, assignedTo: this.userId, status: SessionStatus.Open });
});

And the getOrCreateTenant method (from which the crash occurs) is

export function getOrCreateTenant(subdomain: string): Tenant {
    const user = Meteor.user({ fields: { "tenantName": 1 } }) as User
    const name = user ? user.tenantName || subdomain : subdomain;
    const result = Promise.await(
        TenantsCollection.rawCollection().findOneAndUpdate(
            { name },
            {
                $setOnInsert: {
                    _id: new Mongo.ObjectID().toHexString(),
                    name, sessionCounter: 0, createdAt: new Date
                }
            },
            { upsert: true, returnDocument: 'after' }))
    if (result.ok === 1 && result.value) {
        return result.value;
    } else {
        throw new Meteor.Error('tenants.getOrCreateTenant.fail', JSON.stringify(result.lastErrorObject))
    }
}

The problem occurs on the first line of this function, only when called from publication invoked by simple:rest. Otherwise the publication works fine when invoked from client app.

Regards,
Kevin