jQuery Promise is undefined
jQuery Promise is undefined
我正在尝试使用jQuery的promise对象,以确保在其他代码运行之前完成异步调用。我需要使用Deferred对象,因为我使用SharePoint的executeQueryAsync
来运行查询。我在这里使用Shereen Qumsieh的例子。
我的问题是,在结果处理程序中,jQuery promise总是未定义的,我无法确定原因。以前有人经历过这种情况吗?任何能为我指明正确方向的帮助或线索都将不胜感激。谢谢
查询
function successCallback() {
console.log(this.d.promise());
this.d.resolve(this.list);
}
function failCallback() {
console.log(this.d.promise());
this.d.reject("something bad happened");
}
function getMyList() {
var d = jQuery.Deferred();
console.log(d.promise());
var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle('Documents');
clientContext.load(list);
var o = {d: d, list:list};
clientContext.executeQueryAsync(Function.createDelegate(o, successCallback), Function.createDelegate(o, failCallback));
return d.promise();
}
结果处理程序
var result = getMyList();
console.log(result);// output: undefined
result.done(function(result) { //TypeError: Cannot read property 'done' of undefined
// result is an SP.List because that is what we passed to resolve()!
var list = result;
console.log('Result done hit. result: ' + result);
});
result.fail(function(result) {
// result is a string because that is what we passed to reject()!
var error = result;
console.log('Result fail hit. result: ' + error);
});
更新:
getMyList console.log(d.promise())输出
Object {state: function, always: function, then: function, promise: function, pipe: function…}
successCallback console.log(this.d.promise())输出
Object {state: function, always: function, then: function, promise: function, pipe: function…}
结果处理程序控制台.log(结果)输出
undefined
带时间戳的顺序输出
getMyList ln 449 Promise output 1412279244379: [object Object]
getMyList ln 458 Promise output 1412279244390: [object Object]
Result output 1412279244392: undefined
Result Handler Error time: 1412279244392: TypeError: Cannot read property 'done' of undefined
successCallback Promise output 1412279244569: [object Object]
successCallback Resolve output 1412279244570: [object Object]
仍然不确定问题的确切根本原因,但我确实找到了解决方案。实现该解决方案的主要区别有两个。首先,我使用了SP.RequestExecutor
而不是SP.ClientContext.executeQueryAsync
。然后按照@Roamer-1888的建议,去掉Function.createDelegate()
的废话。SP.ClientContext.executeQueryAsync
不断返回一个从未真正解析过的SP.ClientRequest
对象,而Function.createDelegate
则对promise对象进行了一些奇怪的作用域。还是不确定。
var def = new jQuery.Deferred();
var executor = new SP.RequestExecutor(_spPageContextInfo.siteAbsoluteUrl);
var data = "{ '__metadata': { 'type': 'SP.Data.ListItemEntityTypeFullName' },"
+ "'Title': '" + Title + "',"
+ "'Body': '" + Body+ "'}";
executor.executeAsync({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'<enter_guid_here>')/items",
method: "POST",
body: data,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: CsomSuccess,
error: CsomFail
});
function CsomSuccess(result){
def.resolve(result);
}
function CsomFail(data, errorCode, errorMessage){
def.reject('Failed to get host site. Error:' + errorMessage);
}
return def.promise();
相关文章:
- Jquery - table.row(tr) is undefined
- noty.js is returning undefined
- $routeParams is undefined
- Angular js $scope is Undefined
- $scope is coming as undefined
- Angular - $cookies is undefined
- TypeError: e is undefined - javascript
- “google.maps.MapTypeId is undefined”在FF 14中使用GMAP3时
- chrome.storage.local["mykey"] is undefined
- 回调在 Angular2/Firebase 中生成“TypeError: this is undefined”
- KendoWindow .visible(); is undefined?
- jQuery .forEach() 导致错误,“undefined is not a function”
- 使用“flickrnode”API时出现“TypeError: undefined is not a function”
- "undefined is not a function" onSuccessHandler
- 莫名其妙的“Undefined is not a function”错误 — 由“getElementsById”引起
- Angular js 在传递 promise 时$asyncValidator“Undefined is not a f
- undefined is not a function when evaluating 'this.batman
- “uncatch TypeError: undefined is not a function” 在使用 str.ind
- 谷歌地图API(使用gmapsjs), setCenter抛出“undefined is not a function”
- 在骨干应用视图中出现" Uncaught TypeError: undefined is not a function