简体   繁体   English

如何从Meteor.method返回之前等待子过程结果

[英]How to wait for sub process results before returning from Meteor.method

I'm kinda surprised that the Meteor.method definitions require a result to be returned rather than a callback be called. 我很惊讶Meteor.method定义要求返回结果而不是调用回调。 But so it is! 但是就是这样!

I'm trying to make an RPC method in Meteor that calls mongoose group methods (It didn't look like meteor's data api lets me do it so I'm working around it). 我正在尝试在Meteor中制作一个调用猫鼬组方法的RPC方法(它看起来不像meteor的数据API允许我这样做,所以我正在研究它)。 I have something like this: 我有这样的事情:

Meteor.methods
  getdata: ->
    mongoose = __meteor_bootstrap__.require('mongoose')
    db = mongoose.connect(__meteor_bootstrap__.mongo_url)
    ASchema = new mongoose.Schema()
    ASchema.add({key: String})
    AObject = mongoose.model('AObject',ASchema)
    AObject.collection.group(
      ...
      ...
      (err,doc) -> # mongoose callback function
         # I want to return some variation of 'doc'
    )
    return ??? # I need to return 'doc' here.

My own variation of the code posted above does work...I get calls from my meteor client, the mongoose objects all work their magic. 我自己上面发布的代码的变体确实起作用了...我收到了流星客户端的呼叫,猫鼬对象都发挥了魔力。 But I can't figure out how to get my results to return within the original context. 但是我不知道如何在原始上下文中返回结果。

How can I do this? 我怎样才能做到这一点?


The answer I went with would make my code look like this: 我得到的答案将使我的代码看起来像这样:

require = __meteor_bootstrap__.require
Meteor.methods
  getdata: ->
    mongoose = require('mongoose')
    Future = require('fibers/future')
    db = mongoose.connect(__meteor_bootstrap__.mongo_url)
    ASchema = new mongoose.Schema()
    ASchema.add({key: String})
    AObject = mongoose.model('AObject',ASchema)
    group = Future.wrap(AObject.collection.group,6)
    docs = group.call(AObject,collection,
      ...
      ...
    ).wait()
    return docs

Ah...figured it out. 啊...弄清楚了。 After googling and googling and finding inordinate numbers of comments along the lines of "don't do it that way, use callbacks!", I finally found it: use fibers ! 在谷歌搜索和谷歌搜索并按照“不要那样做,使用回调!”的方式找到过多的评论之后,我终于找到了:使用纤维

I ended up using the fibers-promise library. 我最终使用了fibre-promise库。 My final code looks something like this: 我的最终代码如下所示:

Meteor.methods
  getdata: ->
    promise = __meteor_bootstrap__.require('fibers-promise')
    mongoose = __meteor_bootstrap__.require('mongoose')
    db = mongoose.connect(__meteor_bootstrap__.mongo_url)
    ASchema = new mongoose.Schema()
    ASchema.add({key: String})
    AObject = mongoose.model('AObject',ASchema)
    mypromise = promise()
    AObject.collection.group(
      ...
      ...
      (err,doc) -> # mongoose callback function
         if err
           mypromise.set new Meteor.Error(500, "my error")
           return
         ...
         ...
         mypromise.set mydesiredresults
    )
    finalValue = mypromise.get()
    if finalValue instanceof Meteor.Error
      throw finalValue
    return finalValue

通过一系列示例查看这个惊人的要点

Using the fibers/future module may give you a better API for Meteor as this is what is used internally, and it comes with any vanilla meteor install. 使用fibre / future模块可能会为您提供更好的Meteor API,因为这是内部使用的方法,并且随任何普通流星安装一起提供。

To take your example: 举个例子:

require  = __meteor_bootstrap__.require
Future   = require 'fibers/future'
mongoose = require 'mongoose'

Meteor.methods
  getdata: ->
    db = mongoose.connect(__meteor_bootstrap__.mongo_url)
    ASchema = new mongoose.Schema()
    ASchema.add({key: String})
    AObject = mongoose.model('AObject',ASchema)

    # wrap the method into a promise
    group = Future.wrap(AObject.collection.group)

    # .wait() will either throw an error or return the result
    doc = group(... args without callback ...).wait()

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

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