无法在我的云代码中执行query.find() [英] can't execute query.find() in my cloud code

查看:84
本文介绍了无法在我的云代码中执行query.find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个云代码,可以从"Core"和"HTTP API并对其进行一些逻辑处理.现在我想根据结果发送推送通知,但是代码甚至没有得到执行,这里是一小段代码

I have a cloud code that fetch data from 'Core' & HTTP API and do some logic on it. Now I want to send push notification based on the result but the code doesn't even getting executed here is a snippet of code

.then(function(result) {
  for (var i = 0; i < queryObjects.length; i++) {
    var object = queryObjects[i];

    console.log('This will loop for '+queryObjects.length+' times');

    var pushQuery = new Parse.Query(Parse.Installation);
    pushQuery.equalTo('installationID',object.get('installationID'));
    pushQuery.equalTo('deviceType','ios');
    Parse.Push.send({ 
      where: pushQuery,
      data: {
          alert: "When do we take to outer space",
          badge: "Increment"
        }
      }, {
        success: function() {
          console.log("Push was successful");
        },
        error: function(error) {
          console.error(error);
        }
    });
  }

即使我尝试执行query.find(),也没发生

Even If I tried to execute query.find() nothing happened

.then(function(result) {
      for (var i = 0; i < queryObjects.length; i++) {
        var object = queryObjects[i];

        console.log('This will loop for '+queryObjects.length+' times');

        var pushQuery = new Parse.Query(Parse.Installation);
        pushQuery.equalTo('installationID',object.get('installationID'));
        pushQuery.equalTo('deviceType','ios');
        pushQuery.find({
          success:function(list) {
            console.log("Query Data: "+list);
          },
          error: function(error) {
            console.log("Error: " + error.code + " " + error.message);
          }
        });
      }

推荐答案

这是因为Push.Send函数的异步特性.您正在一个循环中多次调用它,但是您不必等待这些调用中的任何一个从.then块返回之前完成.您需要知道,当Pasre异步函数返回时,它不一定要完成.您需要等待其Promise兑现,以确保它已完成.

This is because of the asynchronous nature of Push.Send function. You are calling it multiple times in a loop but you do not wait for any of those calls to finish before returning from the .then block. You need to know that when a Pasre asynchronous function returns, it is not necessarily finished. You need to wait on its Promise to be fulfilled just to make sure it has completed.

您可以更改代码以确保仅调用一次Parse.Push函数(在安装查询中,对在循环中构建的installationID数组使用containedIn约束,并在之后仅调用一次Parse.Push循环并确保返回其Promise.

You can change your code to make sure that you call Parse.Push function only once (in your Installation query, use containedIn constraint on the array of your installationIDs you build in the loop and call Parse.Push only once after the loop and make sure to return its Promise.

另一种解决方案是,每次在循环中调用Parse.Push函数时,都需要将它们压入一个promise数组中,然后等待所有它们使用Parse.Promise.when(promises);完成.该文档非常清楚如何并行等待Promises.

The other solution is that for each time you call a Parse.Push function in the loop, you need to push them in an array of promises and then wait for all of them to finish using Parse.Promise.when(promises);. The documentation is very clear on how to wait on Promises in parallel.

这篇关于无法在我的云代码中执行query.find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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