0

[Edit] this is a problem specific to flow router, I started an issue here : https://github.com/meteorhacks/flow-router/issues/205 I do not know if this is an actual issue or if I conceived the wrong way..

When called in server startup code, Blog.findOne() returns a value (server side)

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

But when called in a server method by a call from the server, it returns undefined

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

WTF ?

It's called classic way :

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

Same calling code for server and client.

I event tried something like that :

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

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

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

So, what is it here that I did not understand ?

By the way, I can insert, update, and remove in the method. And I get undefined whether I call from the client or from the server. It really puzzle me...

When the method is called client side, it works.

[ EDIT ]

I tried to unblock and wrapAsync (yes, I'm trying anything...)

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

Still working when called from client, still returning undifined when called from server ( Blog.findOne() return undefined, so the call returns an error).

The only difference I can see is that the call from the client has a this.connection and no this.randomSeed.

The Future way keep working client side, but not server side :

Future = Npm.require( 'fibers/future' );
...
getHomeData( v ) {
    this.unblock();
    var myFuture = new Future();
    ( () => {
        myFuture.return( Blog.findOne()._id );
    } )();
    return myFuture.wait();
},
fabien
  • 2,228
  • 2
  • 19
  • 44
  • 1
    Can you `find().fetch()` in your method? – SylvainB Jul 10 '15 at 11:08
  • Nop. This is actually the first thing I tried ; to filter my request. – fabien Jul 10 '15 at 12:59
  • What does entering `Blog.find().fetch()` and pressing enter return in the browser console? – Michael Mason Jul 10 '15 at 13:05
  • `find` works properly in the console. I think this is a sync/async problem. A wrapasync somewhere is probably the solution. But I don't see how exactly. – fabien Jul 10 '15 at 14:13
  • try using a callback to get your data. If you are calling it from a client side, you will get undefined unless a callback is provided. http://docs.meteor.com/#/full/meteor_call read here – Suman Lama Jul 10 '15 at 14:46

0 Answers0