Meteor,流路由器ssr:findOne在流路由器动作中从服务器调用Meteor.methods时返回undefined

时间:2015-07-10 11:03:53

标签: javascript meteor router flow-router

[编辑]这是特定于流路由器的问题,我在这里开始了一个问题:https://github.com/meteorhacks/flow-router/issues/205我不知道这是否是一个实际问题,或者我是否构思错误的方法..

在服务器启动代码中调用时,Blog.findOne()返回一个值(服务器端)

Meteor.startup( function () {
    console.log( Blog.findOne() );
    // one blog post
} );

但是当通过服务器调用在服务器方法中调用时,它返回undefined

Meteor.methods( {
    getHomeData() {
        console.log( Blog.findOne() );
        //undefined

WTF?

它被称为经典方式:

Meteor.call( 'getHomeData', ( err, res ) => {
    console.log( 'server call err :' + err );
    console.log( 'server call res :' + res );
} );

服务器和客户端的相同调用代码。

我的事件尝试了类似的事情:

let test = () => {
    return Blog.findOne();
};

Meteor.startup( function () {
    console.log( test() );
    // still good
} );

Meteor.methods( {
    getHomeData: () => {
        console.log( test() );
        // still undefined WTF!

那么,我不明白这是什么?

顺便说一句,我可以在方法中insertupdateremove。无论我是从客户端还是从服务器调用,我都不明确。这真让我感到困惑......

当该方法被称为客户端时,它可以工作。

[编辑]

我试图取消阻止并包装Async(是的,我正在尝试任何事情......)

getHomeData() {
    this.unblock();
    console.log( 'startCall' );
    return Meteor.wrapAsync( () => {
        console.log( 'start async call' );
        console.log( Blog.findOne()._id );
        return Blog.findOne()._id;
    } )();
},

从客户端调用时仍然正常工作,当从服务器调用时仍然返回unifined(Blog.findOne()返回undefined,因此调用返回错误)。

我能看到的唯一区别是来自客户的来电有this.connection而没有this.randomSeed

Future方式继续工作客户端,但不是服务器端:

Future = Npm.require( 'fibers/future' );
...
getHomeData( v ) {
    this.unblock();
    var myFuture = new Future();
    ( () => {
        myFuture.return( Blog.findOne()._id );
    } )();
    return myFuture.wait();
},

0 个答案:

没有答案