【问题标题】:parse query doesn't return results解析查询不返回结果
【发布时间】:2014-02-20 12:46:25
【问题描述】:

我已经定义了一个解析云代码调用getChat(如下),但是当我运行它时,它不会返回任何结果

Parse.Cloud.define("getChat", function(request, response) {
  var allchat = [];
  var query = new Parse.Query("chat");
  query.find().then(function(results) {
    console.error("test"); //nothing in console
    console.error(results.length); //nothing in console
    for (var i = 0; i < results.length; ++i) {
      for(var iii = 0; iii<results[i].get("limitleft").length; iii+=2){
        if(results[i].get("limitleft")[iii] == request.params.user){
          allchat.push(results[i]);
        }
      }
    }
  });
  response.success(allchat);
});

【问题讨论】:

    标签: parse-platform


    【解决方案1】:

    Query.find() 返回一个 Promise。 ".then" 为该承诺附加了一个回调函数。当 Find 完成时,它会执行回调。但是,您在启动 Find 后立即调用了 response.success()。结果尚未交付。调用 response.success() 有效地取消了查找,因为 getChat 已通过调用 response.success() 完成。

    将对 response.success() 的调用放在块内!

    -鲍勃

    【讨论】:

    • 让代码在后台运行它然后运行下一个代码?
    • 当函数 getChat 运行时,它开始查找一组对象,然后退出。查找仍在运行并且 response.success() 尚未被调用,因此上下文被保留。查找完成后,将运行该块。如果该块随后调用 response.success(),则原始调用将完成并删除上下文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 2019-09-18
    • 2012-05-08
    • 2016-09-30
    相关资源
    最近更新 更多