[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();
},