解析查询查找方法返回对象而不是数组

Parse Query find method returns object not array

本文关键字:数组 对象 返回 查询 查找 方法      更新时间:2023-09-26

我正在使用一个使用 Parse 作为后端的移动应用程序,我对查找功能有问题。以以下格式运行查找函数时:

var = firstQuery = (new Parse.Query("MyParseObject"))
  .find(),
  secondQuery = (new Parse.Query("OtherParseObject")).get(id)
// there is only one object that firstQuery can find
Parse.Promise.when(firstQuery, secondQuery)
  .then( function (query1res, query2res) {
    // query1res should return only one result wrapped in an array,
    // instead query1res is an object without a get method
   query1res.forEach (function (res) {
     // this fails: cannot get .length of undefined
   })  
   // ... do stuff with the returned data

  })

我缺少什么吗?我敢肯定这以前有效,但现在不行了。

由于 Parse 的工作方式,很难正确调试此问题,但他们的文档概述了这应该返回一个数组,但目前没有。

感谢您的帮助。

根据 Parse 文档,看起来Parse.Promise.when需要一个数组,尽管基于此支持线程,结果将作为单独的参数传递给then处理程序。试一试:

Parse.Promise.when([firstQuery, secondQuery])
.then(function (query1res, query2res) {
   // use query1res and query2res
});

事实证明,它归结为代码中更深层次的函数,该函数需要返回一个承诺才能链接。 添加此内容后,代码已经足够高兴了。需要返回承诺的函数是在 forEach 中调用的,与最初的两个查询无关,这就是抛出我的原因。