简体   繁体   中英

Meteor method returns different object on client and server

I have a simple method, which should return all available methods on server back to client, but returned response is an empty object.

Client:

Meteor.call("servMethods", function(err, res) {
  if(err) {
    console.log(err);
  }
  if(res) {
    console.log(res);
    // logs only:
    // Object {}
  }
});

Server:

Meteor.methods({
  "met1": function() {
  },
  "met2": function() {
  },
  "servMethods": function() {
    var methods = Meteor.default_server.method_handlers;
    console.log(methods);
    return methods;

    // logs correctly:
    // { met1: [Function: met1],
    //   met2: [Function: met2],
    //   servMethods: [Function: servMethods] }

  }
});

Server–side methods can only return EJSON–able values to the client. Functions are not EJSON-able, and therefore they're filtered out from the result object.

If you want to send the list of all available method names to the client, filter out the functions yourself, for example:

"servMethods": function() {
  return _.keys(Meteor.default_server.method_handlers);
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM