解析方法。然后和链接|语法躲​​开我 [英] Parse .then method and chaining | Syntax eludes me

查看:188
本文介绍了解析方法。然后和链接|语法躲​​开我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的计算器社区,

我使用的应用工艺作为一个JS云IDE和整合解析的云数据库进行存储。

I am using application craft as a JS cloud IDE and integrating Parse's cloud database for storage.

我正在写一个订阅服务,并要检查,看是否有用户已经不是验证其​​使用或促使他们注册之前认购。作为我试图利用解析的/ Promise的。于是方法等待异步调用继续之前,服务器的响应。

I'm writing a subscription service and want to check to see if a user is already subscribed prior to either authenticating their use or prompting them to sign up. Being an asynchronous call I'm trying to utilise Parse's/Promise's .then method to wait for the server's response before proceeding.

我读过有关解析网站(之前链接)的例子和样品尚不能换我的头周围。

I've read the examples and samples on the Parse site (linked before) yet cannot wrap my head around it.

这code函数,返回如果发​​现匹配的电子邮件地址:

This code functions, and returns the email address if a match is found:

      ... *Parse declaration etc*  
query.find({
            success: function(results){
                if(results.length>0){
                    app.setValue("lblOutput", results[0].attributes.email);
                }
                else{
                    app.setValue("lblOutput", "No match.");
                }
            },
            error: function(object, error){
                console.log(error.message);
                console.log(object);
            }
        });  

我在尝试链接,最近一次是这样的:

My attempt at chaining, most recently this:

    query.find().then(function(results) {
        if(results){
            console.log("Within the Then!");
            console.log(results);
        }
        else{
            console.log("Could be an error");
        }
    });

国指方法或属性的成功是未定义无效。我试图第一个函数的语法结合起来(成功:... //错误:...)。在失败的尝试链接

States that method or property 'success' is invalid for undefined. I have attempted to combine the syntax of the first function (success: ... // error: ...) in the chaining attempt unsuccessfully.

任何意见,至于我怎么会

Any advice as to how I could


  1. 检查,看看是否在分析数据库中存在的邮件,,然后

  2. 等待,直到结果回来作进一步处理与其他功能

将大大AP preciated。

would be greatly appreciated.

一旦我。那么()想通了就会有异步等待进一步的层。

Once I have .then() figured out there will be further layers of async waiting.

干杯,

詹姆斯

推荐答案

您处理那么语法不正确,它应该是:

Your syntax for handling then is incorrect, it should be:

query.find().then(

    function(results) {    
        console.log("Within the Then!");
        console.log(results);    
    },

    function(error){
        console.log("Could be an error"+error);
    }
);

当时()函数有一个或两个功能。

the then() function takes one or two functions.

第一是成功的处理程序,第二个是一个错误处理程序。

the first is a success handler, and the second is an error handler.

query.find()。然后,(成功,错误)

query.find().then(success,error)

这片段是未经检验的,但应该是pretty关闭。

This snippet is untested but should be pretty close.

var query = new Parse.Query(Parse.User);
query.equalTo(email, "me@me.com");  
query.count().then(
    function(resultsCount) {    
        //success handler
        if(resultsCount > 0){
            doSomeOtherFunction();
        }
    },
    function(error){
        //error handler
        console.log("Error:"+error);
    }
);

如果你有更多的异步工作要做,你的方法应该类似于此,请记住,承诺链的最后一个时,则()应该包含您的错误处理。

If you have more async work to do, your method should look similar to this, remember that when chaining promises the last then() should contain your error handling.

query.find().then(function(result){
  doSomethingAsync(); //must return a promise for chaining to work!
}).then(function(result1){
  doSomethingAsync2(); //must return a promise for chaining to work!
}).then(function(result2){
  doSomethingAsync3(); //must return a promise for chaining to work!
}).then(null,function(error){
  // an alternative way to handle errors
  //handles errors for all chained promises.
})

这篇关于解析方法。然后和链接|语法躲​​开我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆