有没有人有一个实际有效的解析承诺的例子?(云API)

bha*_*ral 2 jquery promise parse-platform

我正在尝试使用解析云API来获得parse.com的新功能〜使用解析云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");
    }
);
Run Code Online (Sandbox Code Playgroud)

无论出于何种原因,此代码都不起作用

我收到错误:

{"code":141,"error":"TypeError:无法调用方法'然后'未定义\n在getModuleIdIfAny(main.js:71:4)\n at main.js:50:2"}

哪个指向读取的行 ).then(

如果你能提供答案,我的笔记本电脑会爱你,否则它必须快速发展翅膀.

Los*_*ees 6

这里有一些有用的代码.

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;
};
Run Code Online (Sandbox Code Playgroud)

我不确定你为什么失败.此代码有效并且非常有用,因为通过ID检索对象的标准调用不会返回promise.我以这种方式命名我的承诺并使用你看到的日志记录语句,以便我可以跟踪日志中的执行情况.我已经成长为真正的承诺,甚至创建了一个与iOS一起使用的Objective-C版本.

祝好运.

-Bob