简体   繁体   English

流星,链接方法调用

[英]Meteor, chaining method calls

I have a Meteor method that tries to manipulate the database, and if successful, calls an async method. 我有一个尝试处理数据库的Meteor方法,如果成功,则调用异步方法。 I would like to be able to call this method and return the result of the async call or the error from the database manipulation. 我希望能够调用此方法,并从数据库操作中返回异步调用或错误的结果。

This is (roughly) my code on the server: 这大概是我在服务器上的代码:

Meteor.methods({
    'data.update'(id, data) {
        Collection.update({id_: id}, {$set: {data: data}}, error => {
            if (error) {
                // Have method return Meteor error for DB-failure
            } else {
                callAsync(id, (error, res) => {
                    if (error) {
                        // Have method return Meteor error for async call failure
                    } else {
                        // Have method return success(res)
                    }
                })
            }
        })
    }
});

I have read about Futures and Promises, but I'm new to the concepts and I'm not sure when to use what. 我已经阅读了有关期货和承诺的文章,但是我对这些概念并不熟悉,因此我不确定何时使用。 Preferably I'm looking for a solution that does not rely on any third party libraries outside of Meteor/ES6. 最好是我正在寻找不依赖Meteor / ES6以外的任何第三方库的解决方案。 Bonus (related) question: what is normally returned after a database manipulation that let's me attach a callback to a method? 额外的(相关的)问题:数据库操作(通常让我将回调附加到方法上)通常返回什么?

According to docs 根据文档

On the server, if you don't provide a callback, then update blocks until the database acknowledges the write, or throws an exception if something went wrong. 在服务器上,如果不提供回调,请更新块,直到数据库确认该写入为止;否则,如果发生错误,则引发异常。 If you do provide a callback, update returns immediately. 如果您提供回调,则update将立即返回。 Once the update completes, the callback is called with a single error argument in the case of failure, or a second argument indicating the number of affected documents if the update was successful. 更新完成后,如果失败,则使用单个错误参数调用回调,如果更新成功,则使用第二个参数指示受影响的文档数。

So, if the update is successful, the number of documents affected is returned. 因此,如果update成功,则返回受影响的文档数。 In case of insert , the _id of the inserted document is returned. 如果是insert ,则返回插入文档的_id

You can simply pass a third argument to the update function as mentioned. 您可以简单地将第三个参数传递给update函数,如上所述。

For promise implementation, you can use Meteor.wrapAsync method. 对于promise实现,可以使用Meteor.wrapAsync方法。 You may also want to look at Meteor.bindEnvironment to achieve that, if you need to pass the state of instance variables too. 如果还需要传递实例变量的状态,则可能还需要查看Meteor.bindEnvironment来实现。

You can look into using Promises, but there is another fairly standard way of handling something like this within the Meteor ecosystem. 您可以考虑使用Promises,但是在Meteor生态系统中还有另一种相当标准的方式来处理类似的事情。 You can use Meteor.wrapAsync to convert your asynchronous callback based function into a Fibers based version. 您可以使用Meteor.wrapAsync将基于异步回调的函数转换为基于Fibers的版本。 This will then let you leverage return values and exceptions. 这样,您就可以利用返回值和异常。 Here's a quick example: 这是一个简单的示例:

1) Let's say we have an internal function somewhere called increaseLifetimeWidgetCount that increases our lifetime widget count in the database somewhere, then calls a callback with either an error or the newly updated lifetime count: 1)假设我们有一个内部功能的地方叫increaseLifetimeWidgetCount增加我们的有生之年部件数据库计算的地方,然后调用与任何错误或新更新的生命周期计数的回调:

function increaseLifetimeWidgetCount(callback) {
  // ...
  // increase the lifetime widget count in the DB somewhere, and 
  // get back the updated widget count, or an error.
  // ...
  const fakeError = null;
  const fakeLifetimeWidgetCount = 1000;
  return callback(fakeError, fakeLifetimeWidgetCount);
}

2) Let's say we then have a simple Method defined that will create a new Widget in our database, call our internal increaseLifetimeWidgetCount function, then return the newly updated lifetime widget count. 2)假设我们定义了一个简单的方法,该方法将在数据库中创建一个新的小部件,调用内部的内部方法increaseLifetimeWidgetCount ,然后返回新近更新的生命周期小部件计数。 Since we want to return the updated lifetime widget count, we'll wrap our callback based increaseLifetimeWidgetCount function in a Meteor.wrapAsync call, and return the result: 因为我们要返回更新的生命周期小部件计数,所以我们将基于回调的increaseLifetimeWidgetCount生命时间控件计数函数包装在Meteor.wrapAsync调用中,并返回结果:

Meteor.methods({
  newWidget(data) {
    check(data, Object);
    Widgets.insert(data);
    const increaseLifetimeWidgetCountFiber =
      Meteor.wrapAsync(increaseLifetimeWidgetCount);
    const lifetimeWidgetCount = increaseLifetimeWidgetCountFiber();
    return lifetimeWidgetCount;
  }
});

3) We can then call the newWidget Method from our client, with an asynchronous callback, and handle either the returned error or returned lifetime widget count: 3)然后,我们可以使用异步回调从客户端调用newWidget方法,并处理返回的错误或返回的生命周期小部件计数:

Meteor.call('newWidget', { 
  name: 'Test Widget 1' 
}, (error, result) => { 
  // Do something with the error or lifetime widget count result ...
  console.log(error, result);
});

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

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