我如何在 meteor 中接收辅助函数的承诺值?

How do I receive promise values in meteor for helper functions?

我想和 Shopify's address library 一起工作。由于这些与承诺一起工作,我考虑过实施回调以接收结果

import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict'
import AddressFormatter from '@shopify/address';

import './main.html';

const address = {
    company: 'Shopify',
    firstName: '恵子',
    lastName: '田中',
    address1: '八重洲1-5-3',
    address2: '',
    city: '目黒区',
    province: 'JP-13',
    zip: '100-8994',
    country: 'JP',
    phone: '',
};

Template.hello.onCreated(function () {
    const addressFormatter = new AddressFormatter('ja');
    const instance = this
    instance.state = new ReactiveDict()
    instance.state.setDefault('result', {
        "formattedAddress": "",
        "orderedFields": ""
    });

    getData(addressFormatter, function(r) {
        // the next line triggers the helper, since it "observes" the changes
        // to this "result" property on the reactive-dictionary
        instance.state.set('result', {
            formattedAddress: r.formattedAddress,
            orderedFields: r.orderedFields
        });
    });
})

Template.hello.helpers({
    address: function() {
        console.log(Template.instance().state.get("result"));
        return Template.instance().state.get('result')
    }
});

function getData(addressFormatter, callback) {
    const fa = async () => {
        const result = await addressFormatter.format(address);
        console.log(result)
        return result;
    }
    const of = async () => {
        const promise = addressFormatter.getOrderedFields('CA');
        promise.then(result => {
            console.log(result);
            return result;
        });
    }
    let results = {
        "formattedAddress": fa(),
        "orderedFields": of()
    }
    callback(results);
}

我在模板中收到的唯一内容是 [object Promise]。 getData() 方法中的 console.logs 实际上显示了准确的数据,但它们没有显示在 teamplte 中。我该怎么做才能接收这些值并让我的助手等待它们?

编辑:我已经根据@Jankapunkt 的回答编辑了它,但是对象仍然是空的,而 getData() 中的结果不是。

你不知道。助手可以立即获得 return 值,但由反应性数据源触发。

如果您希望助手在数据“到达”后“运行”,那么您应该将此代码移至 onCreated 并将值存储在响应式数据源中:

import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict'
import AddressFormatter from '@shopify/address';

import './main.html';

const address = {
    company: 'Shopify',
    firstName: '恵子',
    lastName: '田中',
    address1: '八重洲1-5-3',
    address2: '',
    city: '目黒区',
    province: 'JP-13',
    zip: '100-8994',
    country: 'JP',
    phone: '',
};

Template.hello.onCreated(function () {
  const instance = this
  instance.state = new ReactiveDict()
  instance.state.setDefault('result', {
    "formattedAddress": "",
    "orderedFields": ""  
  })


  const addressFormatter = new AddressFormatter('ja')
  getData(addressFormatter)
    .then(({ formattedAddress, orderedFields }) => {
      // the next line triggers the helper, since it "observes" the changes
      // to this "result" property on the reactive-dictionary
      instance.state.set('result', { formattedAddress, orderedFields })
    })
    .catch(e => console.error(e))

  return results;
})

Template.hello.helpers({
    address: function() {
      return Template.instance().state.get('result')
    }
});

const getData = async function (addressFormatter) {
  const formattedAddress = await addressFormatter.format(address)
  const orderedFields = await addressFormatter.getOrderedFields('CA')
  return {
    formattedAddress,
    orderedFields
  }
}

读数:http://blazejs.org/

编辑:添加了应该有效的简化 getData