[英]Worklight JSONStore result not returned
我有一個函數,如果成功,該函數應該返回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
有人知道如何解決?
您似乎期望異步代碼同步運行。 嘗試添加回調,一些偽代碼:
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));
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.