简体   繁体   English

Meteor方法调用在客户端上返回undefined但在服务器上没有返回

[英]Meteor method call returns undefined on the client but not on the server

UPDATE UPDATE

I just realized something fundamentally wrong in this approach and that nested callbacks can't return something to its parent callback. 我刚刚意识到这种方法存在根本性的错误,并且嵌套的回调不能向其父回调返回一些内容。 I came in late in the JS world and come from the Promises era and didn't know this is the problem with callbacks. 我在JS世界中来得很晚,来自Promises时代,并不知道这是回调的问题。 But I didn't see enough examples for Meteor using promises so I used callbacks instead. 但我没有看到Meteor使用promises的足够例子,所以我使用了回调。 However, if this code can be improved I'd appreciate it greatly. 但是,如果这个代码可以改进,我会非常感激。

Question

So I'm calling a method from the client using: 所以我正在使用以下方法从客户端调用方法:

Meteor.call('cart.useProfileAddress', {}, (error, address) => {
  console.info('Address', address) // this returns undefined on client
})

This is the method in my api/carts/cartsMethod.js 这是我的api/carts/cartsMethod.js

export const useProfileAddress = new ValidatedMethod({
  name: 'cart.useProfileAddress',
  validate(args) {
    //
  },
  run(args) {
    const person = Persons.findOne({'userId': Meteor.userId()});
    // If I do the return here I get the address in the browser as defined.
    // return person.address

    // I'm calling another method under here:
    getClosestStore.call({address: person.address}, (error, result) => {
      // And another one method call here:
      updateCartAddress.call({address: person.address}, (error, result) => {
        // So once all the callbacks are done return the address here.
        // However the problem is I get `undefined` on the client.
        if (!error) {
          // console displays something on the Server but is `undefined` on the Client
          console.info('Returning Address', person.address)
          return person.address
        }
      })
    })
  }
})

What could be the problem on the code above? 上面的代码可能有什么问题? Could it be because I'm trying to get the value from a nested callback? 可能是因为我试图从嵌套回调中获取值吗?

Also does anyone know how to avoid these nested callbacks? 也有人知道如何避免这些嵌套回调吗? I know how to do it on Node using promises but in Meteor (I'm using 1.4 ) I'm still clueless. 我知道如何使用promises在Node进行,但在Meteor中(我使用的是1.4 )我仍然无能为力。

Methods can run synchronously on server so you do not need to use callback. 方法可以在服务器上同步运行,因此您不需要使用回调。 Result of method will be returned after execution or exception will be thrown if error occurs. 执行后将返回方法的结果,否则将在发生错误时抛出异常。 Try this: 试试这个:

export const useProfileAddress = new ValidatedMethod({
  // ...
  run(args) {
    const person = Persons.findOne({'userId': Meteor.userId()});

    const result1 = getClosestStore.call({address: person.address});

    // use result1 if you need to

    const result2 = updateCartAddress.call({address: person.address});

    // // use result2 if you need to

    return person.address;
  }
})

This is how I solved my problem using Promise and the new async/await feature of Meteor 1.3+ 这就是我使用Promise和Meteor 1.3+的新async/await功能解决我的问题的方法

export const useProfileAddress = new ValidatedMethod({
  name: 'cart.useProfileAddress',
  validate(args) {
    //
  },
  run(args) {
    return ((async () => {
      const person = Persons.findOne({'userId': Meteor.userId()});
      const storeId = await getClosestStore.callPromise({address: person.address})
      const newAddress = await updateCartAddress.callPromise({address: person.address})

      return newAddress
    })())
  }
})

Inside each method I used the didericis:callpromise-mixin so that it will return a promise. 在每个方法中,我使用了didericis:callpromise-mixin,以便它返回一个promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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