Obtenga un resultado del método del servidor antes de ejecutar una transformación de colección

Trabajo sobre CoinsManager, I have a model directory with a class per file, and I want to read and list all those files in my collection transform method, to initialize my doc with the correct class.

server/methods.coffee:

Meteor.methods
  implemented_coins: ->
    """
    Returns a list of coins that have been implemented
    """
    files = fs.readdirSync './app/models/cryptos/'
    file.replace(".coffee.js", "") for file in files.filter (file) ->
      file.search("(base_crypto*)|(js.map)") == -1

collections/addresses.coffee:

if Meteor.isReady
  @implementedCoins = Meteor.call "implemented_coins"

@Addresses = new Meteor.Collection "addresses",
  transform: (doc) ->
    # Retrieve class from code, and pass it the address
    if doc.code in @implementedCoins
      new @[doc.code] doc.address
    else doc

client/views/addresses/addresses_list.coffee

Template.userAddresses.helpers
  userAddresses: ->
    addresses = Addresses.find
      userId: Meteor.user()._id
    address.set_balance() for address in addresses
    return addresses

Right now, I'm getting the following error on the client console:

Exception from Deps recompute: TypeError: Array.prototype.indexOf called on null or undefined
    at indexOf (native)
    at Addresses.Meteor.Collection.transform

Which means that in my collection transform, the @implementedCoins variable is undefined, because I didn't implement it correctly.

¿Alguna idea de cómo resolver este problema?

preguntado el 12 de febrero de 14 a las 05:02

1 Respuestas

I'm pretty sure that this is wrong:

if Meteor.isReady
  @implementedCoins = Meteor.call "implemented_coins"

I don't think there is a field in Meteor with that name, and even if it was, then it would get executed on startup, but at that time isReady is probably false and so your variable doesn't get set. Did you mean Meteor.startup? Secondly, on the client you need to use a callback for call, since there are no fibers on the client.

¿Funcionaría esto en su lugar?

Meteor.startup(function () {
    Meteor.call("implemented_coins", function(err, res) { 
         implementedCoins = res; 
    });
});

Respondido 12 Feb 14, 05:02

Thank you for the fast answer :) yes, it seems to work with your method (other bug to fix, but it doesn't seem related). I'm a bit confused as to why the callback is called before the address transform, can you explain it ? - adrian lemaire

not sure, but it's probably to do with the collection not being ready yet. It will actually be called initially on page load, but at that time the collection is empty and so the transform doesn't apply. It is then re-run when the collection is synced to the client. - Christian Fritz

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.