Meteor.userId()在ConnectHandlers中不可用

时间:2015-09-21 19:51:21

标签: meteor

我正在尝试在Meteor中创建文件上传功能,其中登录用户能够将文件上传到以其用户名命名的目录下的服务器。我有基本的工作,但当我通过检查登录的用户ID更进一步,事情开始打破。具体做法是:

public static void main(String[] args) {
    HashMap<Class<? extends Packet>, HandlerOne<? extends Packet>> handlerMap = new HashMap<>();
    handlerMap.put(PacketOne.class, new HandlerOneImpl());

    ServiceImpl<Packet, HandlerOne<?>> s = new ServiceImpl<>(handlerMap);//ERROR
}

public static abstract class Service<E extends Packet, T extends Handler<E>> {

    Map<Class<? extends Packet>, T> handlerMap;

    public Service(Map<Class<? extends Packet>, T> handlerMap) {
        this.handlerMap = handlerMap;
    }

    //Some random logic

    //If only I could use T<E>
    protected abstract void handle(T handler, E packet);

}

public static class ServiceImpl<E extends Packet, T extends HandlerOne<E>> extends Service<E, T> {

    public ServiceImpl(Map<Class<? extends Packet>, T> handlerMap) {
        super(handlerMap);
    }

    @Override
    protected void handle(T handler, E packet) {
        handler.handle("someObject", packet);
    }

}

结果:

WebApp.connectHandlers.use('/upload/', function(req, res) {
    if (this.userId) {
        // Do cool stuff.
    } else {
        res.writeHead(500, {"content-type":"text/html"});
        res.end("this.userId = " + this.userId); // End the response.
    }
});

和...

this.userId = undefined

结果:

WebApp.connectHandlers.use('/upload/', function(req, res) {
    if (Meteor.userId()) {
        // Do cool stuff.
    } else {
        res.writeHead(500, {"content-type":"text/html"});
        res.end("Meteor.userId() = " + Meteor.userId()); // End the response.
    }
});

上面的代码包含在我正在开发的Meteor包中。 package.js文件指定代码应该在服务器上运行:

Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
    at Object.Meteor.userId (packages/accounts-base/accounts_server.js:19:1)
    at Object.Package [as handle] (packages/cool_package/upload.js:34:1)
    at next (/Users/me/.meteor/packages/webapp/.1.2.0.19shc3d++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:190:15)
    at Function.app.handle (/Users/me/.meteor/packages/webapp/.1.2.0.19shc3d++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:198:3)
    at Object.fn [as handle] (/Users/me/.meteor/packages/webapp/.1.2.0.19shc3d++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:74:14)
    at next (/Users/me/.meteor/packages/webapp/.1.2.0.19shc3d++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:190:15)
    at Object.WebAppInternals.staticFilesMiddleware (packages/webapp/webapp_server.js:331:1)
    at packages/webapp/webapp_server.js:625:1

所以我的问题是:

  • 检查登录用户ID和用户名的正确方法是什么?
  • 可以将此代码转移到Iron Router路由吗?

1 个答案:

答案 0 :(得分:1)

看起来像

WebApp.connectHandlers.use('/upload/', function(req, res) {

Express.js或类似的代码 - 如果是这样,你已经打破了提供自己的REST服务的Meteor frameowrk等。如果是这种情况,你还必须为传入的REST提供自己的用户管理和身份验证方案调用,就像在任何其他简单的REST应用程序中一样