How to get Meteor.user() to return on the server side?

前端 未结 4 1300
名媛妹妹
名媛妹妹 2020-12-13 06:50

in a file called /server/main.js (in order to ensure it is loaded last).

console.dir(Meteor.user());

Throws:

Error: Meteor.         


        
4条回答
  •  无人及你
    2020-12-13 07:16

    You can expose the userId with Meteor.publish() to global scope. Then you can use it with Meteor.Router's server side routes.

    --

    /server/publications.js

    CurrentUserId = null;
    Meteor.publish(null, function() {
        CurrentUserId = this.userId;
    });
    

    -

    /server/routes.js

    Meteor.Router.add('/upload', 'POST', function() {
        if (!CurrentUserId)
            return [403, 'Forbidden'];
    
        // proceed with upload...
    });
    

提交回复
热议问题