jquery - 有人有一个实际有效的解析 promise 的例子吗? (云API)

标签 jquery parse-platform promise

我正在尝试让 parse.com 的奇特新 promise 发挥作用 ~ 我的意思是使用解析云 API。可悲的是,我遇到了一些麻烦 - 我找不到任何关于如何使其工作的真正讨论(API 似乎都是谎言或无能)

这是我的代码:

query.find(
    function(results){
        if (results.length > 0){
            return results[0].get("id");
        } else {
            response.error("Nothing found. Sod this.");
        }
    },
    function(error){
        response.error("ServerDown");
    }
).then(
    function(moduleId){
    //do anything here, i don't know, increment module? Who cares!
    },
    function(error){
        console.log("error");
    }
);

无论出于何种原因,此代码都无法工作

我收到错误:

{“code”:141,“error”:“TypeError:无法在 getModuleIdIfAny (main.js:71:4)\n at main.js:50:2 调用未定义\n的方法“then””}

它指向读取).then(

的行

如果你能提供答案,我的笔记本电脑就会爱你,否则它必须快速进化翅膀。

最佳答案

这是一些有效的代码。

exports.lookupXByID = function (object)
// Must fulfill with an error when this lookup fails
// Sucessful returns must return X
{ 
    console.log("lookupXByID.entry");

    var X = Parse.Object.extend("X");
    var query = new Parse.Query(X);

    console.log("lookupXByID.query(" + object.id + ")");
    query.equalTo("objectId", object.id);
    var promise0 = query.first();
    var promise1 = promise0.then(
        function(result) {
            console.log("lookupXByID.promise0.success");
            if (typeof result === "undefined") {
                return utilMod.promiseerror(10004, "No matching X ID");
            } else {
                // We have a match
                return result;
            };
        }
    );
    console.log("lookupXByID.exit");
    return promise1;
};

我不确定你的失败原因。该代码可以工作并且很有用,因为通过 ID 检索对象的标准调用不会返回 promise 。我以这种方式命名我的 Promise,并使用您所看到的日志语句,以便我可以跟踪日志中的执行情况。我已经变得非常喜欢 Promise,甚至创建了一个用于 iOS 的 Objective-C 版本。

祝你好运。

-鲍勃

关于jquery - 有人有一个实际有效的解析 promise 的例子吗? (云API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17136783/

相关文章:

jQuery 全日历 : contextmenu

javascript - 调用/加载Jquery页面

ios - 完成 block Swift 后设置标签文本

javascript - Firebase .orderByChild().equalTo().once().then() 当 child 不存在时 promise

javascript - 如何在 readystatechange 上 promise AJAX 请求?

javascript - 与我的 jquery 弹出窗口和自动调整文本区域大小发生冲突

javascript - jquery鼠标点击事件

ios - PFObject 的 saveInBackgroundWithBlock : method - the block doesn't get called

ios - 如何使用 Parse 保存数组?

typescript - promise 只是返回?