解析:云代码变量声明

时间:2015-11-27 17:06:08

标签: javascript parse-platform cloud-code

我不确定这里出了什么问题/如何解决它。

Parse.Cloud.job("expireTimes", function(request, response) {
var currentTime = new Date().getTime() / 1000;
var Wait = Parse.Object.extend("Wait");
Parse.Object.registerSubclass("Wait", Wait);
var query = new Parse.Query(Wait);
query.select(["times", "timestamp"]);
query.find({
    success: function(results) {
        for (var i = 0; i < timestamps.length; i++) {
            if (currentTime >= timestamps[i] + 60) {
                // Delete old wait times
                timestamps.splice(i, 1);
                times.splice(i, 1);
                i--;
            } else {
                break;
            }
        };
        response.success("Got " + results.length + " Query results: " + results[0].objectId);

    },
    error: function(error) {
        response.error("Request failed with response code");
        console.error("Request failed with response code ");
    }
});
});

它告诉我时间戳是未定义的:

I2015-11-27T17:00:03.419Z]Deployed v17 with triggers:
Jobs:
    expireTimes
E2015-11-27T17:01:04.489Z]v17 Ran job expireTimes with:
  Input: {}
  Result: ReferenceError: timestamps is not defined
    at e.query.find.success (main.js:9:24)
    at e.<anonymous> (Parse.js:14:28224)
    at e.s (Parse.js:14:27156)
    at e.n.value (Parse.js:14:26575)
    at e.s (Parse.js:14:27284)
    at e.n.value (Parse.js:14:26575)
    at e.s (Parse.js:14:27284)
    at e.n.value (Parse.js:14:26575)
    at e.<anonymous> (Parse.js:14:27228)
    at e.s (Parse.js:14:27156)

不确定这意味着什么/如何解决它。我会假设声明时间戳,但我认为已经完成了在“时间”和“时间戳”列中检索数组

非常感谢任何帮助。 谢谢

1 个答案:

答案 0 :(得分:1)

没有query.select()这样的想法。完成函数中唯一定义的变量是结果(检索到的解析对象的数组)和currentTime,它们是在封闭范围内定义的。

除非您实际定义了子类,否则registerSubclass没用。

作为一个开始练习,请尝试查询该类并记录结果。查询可以简单如下:

var query = new Parse.Query("Wait");
query.find().then(function(results) {
    console.log(JSON.stringify(results));
    var firstId = (results.length>0)? results[0].id : "n/a";  // notice "id", not "objectId"
    console.log("Got " + results.length + " First id: " + firstId);
    response.success(results);
}, function(error) {
    response.error(error);  // return the real error so we can look at it
});

如果timestamp是Wait类的已定义属性,则可以使用get()...

获取它
// just like before where you got the id, try "getting" an attribute
var firstTimestamp = (results.length>0)? results[0].get("timestamp") : "n/a";
// remember, this depends on you defining a "timestamp" attribute on the class