【问题标题】:Worklight JSONStore result not returned未返回 Worklight JSONStore 结果
【发布时间】:2014-06-29 20:27:49
【问题描述】:

我有一个函数,如果成功,它应该返回 JSONStore 查询的结果数组。由于某种原因,它没有返回结果。

这是我的功能

function getJSON(){
var collectionName = 'messages';
var query = {title: 'asdf'};
var options =  {
        exact:  false,
        limit:  10
};
result = [];

WL.JSONStore.get(collectionName)
.find(query, options)
.then(function (ArrayResult) {
    result = ArrayResult;
    console.log(result);
    console.log(typeof result);
    return result;
})
.fail(function (errrorObject){
    console.log("could not get JSONStore: \n" + errrorObject);
});

}

这就是它的名字:

$("#button").click( function() {
   console.log("type of returned result in buttonClick: " + typeof getJSON());
})

控制台输出也以奇怪的顺序出现:

 "weird orderd output" CDMS_Demo.js:96
 "type of returned result in buttonClick: undefined" CDMS_Demo.html:57
 "result in getJSON: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]" CDMS_Demo.js:89
 "type of result in getJSON: object" CDMS_Demo.js:90

有人知道如何解决它吗?

【问题讨论】:

    标签: ibm-mobilefirst jsonstore


    【解决方案1】:

    您似乎期望异步代码同步运行。尝试添加回调,一些伪代码:

    function getJSON (callback) {
    
      var collectionName = 'messages';
    
      var query = {title: 'asdf'};
    
      var options =  {
              exact:  false,
              limit:  10
      };
    
      WL.JSONStore.get(collectionName)
    
      .find(query, options)
    
      .then(function (arrayResult) {
          console.log(JSON.stringify(arrayResult));
          callback(arrayResult);
      })
    
      .fail(function (errorObject){
          console.log(errorObject.toString());
      });
    }
    
    $("#button").click(function () {
    
      getJSON(function (arrayResult) {
        console.log(JSON.stringify(arrayResult));
      });
    });
    

    【讨论】:

    • JSONStore 是一个异步 API,这就是为什么使用它的代码是异步的。请点击答案旁边的绿色复选标记,让其他用户知道这是一个“已接受的答案”。
    猜你喜欢
    • 2013-01-13
    • 1970-01-01
    • 2013-02-15
    • 2013-08-13
    • 2014-08-10
    • 2014-09-19
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多