meteor.js - 如何从异步回调中检查值

时间:2020-12-29 06:59:32

CONTEXT

CONTEXT

Im making a call which, if successful, changes the value of a boolean from false to true. Then, outside of this call, I check if this boolean is true and, if so, I route to another page.

我正在进行调用,如果成功,则将布尔值从false更改为true。然后,在这个调用之外,我检查这个布尔值是否为真,如果是,我将路由到另一个页面。

PROBLEM

问题

Console logs indicate that the if statement which checks the boolean's value is being executed before the calls have had a time to change the boolean's value. I realize that this is because of asynchronicity, but not sure what the correct design pattern for this would be. Here is a snippet:

控制台日志指示在调用有时间更改布尔值之前,正在执行检查布尔值的if语句。我意识到这是因为异步性,但不确定这个的正确设计模式是什么。这是一个片段:

     //set variables to check if the even and user get updated or if error
    var eventUpdated = false;

     Meteor.call('updateEvent', eventId, eventParams, function(error, result){
      if(error){
        toastr.error(error.reason)
      } else {
        var venueId = result;
        toastr.success('Event Info Updated');  
        eventUpdated = true;

        console.log(eventUpdated)
      }               
    });

    console.log(eventUpdated)


     if (eventUpdated) {
         Router.go('/get-started/confirmation');
     }

POSSIBLE SOLUTIONS

可能的解决方案

I'm guessing I need a way to hold the if statement from being executed until the callback returns a value. Based on Googling, I think this has something to do with this but not too clear on how to actually use it.

我猜我需要一种方法来保持if语句被执行,直到回调返回一个值。基于谷歌搜索,我认为这与此有关,但不太清楚如何实际使用它。

2 个解决方案

#1


2  

Since the conditional is run before the callbacks have returned a value, you need a conditional that is inside a function that is being run reactively. I used the following code:

由于条件在回调返回值之前运行,因此您需要一个在响应式运行的函数内的条件。我使用了以下代码:

    Tracker.autorun(function(){
        if (Session.get('userUpdated') && Session.get('passwordUpdated') && Session.get('eventUpdated')) {
            Router.go('/get-started/confirmation');
        }
    });

You can read more about Meteor reactivity here.

你可以在这里阅读更多关于流星反应的信息。

#2


1  

Nope. The issue is that since it's an async function, this:

不。问题是因为它是一个异步函数,所以:

console.log(eventUpdated)


     if (eventUpdated) {
         Router.go('/get-started/confirmation');
     }

Runs before the actual call. Use a Session.set inside the call like this:

在实际通话之前运行。在调用中使用Session.set,如下所示:

Session.set("eventUpdated", "true");

And then outside:

外面:

eventUpdated = Session.get("eventUpdated");

console.log(eventUpdated)


     if (eventUpdated) {
         Router.go('/get-started/confirmation');
     }

Since Session is a reactive variable you should get the current value correctly.

由于Session是一个反应变量,你应该正确获得当前值。

#1


2  

Since the conditional is run before the callbacks have returned a value, you need a conditional that is inside a function that is being run reactively. I used the following code:

由于条件在回调返回值之前运行,因此您需要一个在响应式运行的函数内的条件。我使用了以下代码:

    Tracker.autorun(function(){
        if (Session.get('userUpdated') && Session.get('passwordUpdated') && Session.get('eventUpdated')) {
            Router.go('/get-started/confirmation');
        }
    });

You can read more about Meteor reactivity here.

你可以在这里阅读更多关于流星反应的信息。

#2


1  

Nope. The issue is that since it's an async function, this:

不。问题是因为它是一个异步函数,所以:

console.log(eventUpdated)


     if (eventUpdated) {
         Router.go('/get-started/confirmation');
     }

Runs before the actual call. Use a Session.set inside the call like this:

在实际通话之前运行。在调用中使用Session.set,如下所示:

Session.set("eventUpdated", "true");

And then outside:

外面:

eventUpdated = Session.get("eventUpdated");

console.log(eventUpdated)


     if (eventUpdated) {
         Router.go('/get-started/confirmation');
     }

Since Session is a reactive variable you should get the current value correctly.

由于Session是一个反应变量,你应该正确获得当前值。