weixin_33720956 2015-01-02 12:47 采纳率: 0%
浏览 24

Ajax承诺不起作用

I'm trying to use promise to return a comparison of current logged in user and a field from a list in SharePoint.

function compareCurrentUserWithListObject() {
   var userProp = this._userProfileProperties;
   var userName = userProp.get_userProfileProperties()['UserName'];

   return this._list.filter(function (element, index, array) {
    var promise = jQuery.ajax({
       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
       type: "GET",
       headers: { "Accept": "application/json;odata=verbose" }
     });
     promise.done(function(data) {
        return (data.d.Email.indexOf(userName) > -1);
     });
   });
}   

function init() {
   var userArray = this.compareCurrentUserWithListObject();
   userArray.done(function(res) {
     if (res.length > 0) {
       //Do some stuff after compare...
     }
   });
}

I'm not sure I'm using the .done correct here. Can someone help me?

EDIT:

Working code:

function compareCurrentUserWithListObject() {
   var userProp = this._userProfileProperties;
   var userName = userProp.get_userProfileProperties()['UserName'];

   return this._list.filter(function (element, index, array) {
    var promise = jQuery.ajax({
       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
       type: "GET",
       headers: { "Accept": "application/json;odata=verbose" }
     });
     promise.done(function(data) {
        return (data.d.Email.indexOf(userName) > -1);
     });
    return promise;
   });
}   

function init() {
   var userArray = this.compareCurrentUserWithListObject();
     if (userArray.length > 0) {
       //Do some stuff after compare...
     }
}
  • 写回答

2条回答 默认 最新

  • YaoRaoLov 2015-01-02 12:54
    关注

    you need to return the promise

    function compareCurrentUserWithListObject() {
       var userProp = this._userProfileProperties;
       var userName = userProp.get_userProfileProperties()['UserName'];
    
       return this._list.filter(function (element, index, array) {
        var promise = jQuery.ajax({
           url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
           type: "GET",
           headers: { "Accept": "application/json;odata=verbose" }
         });
         promise.done(function(data) {
            return (data.d.Email.indexOf(userName) > -1);
         });
         // return promise here
         return promise;
       });
    }   
    

    or this (which is cleaner IMO):

    function compareCurrentUserWithListObject() {
       var userProp = this._userProfileProperties;
       var userName = userProp.get_userProfileProperties()['UserName'];
    
    
        return jQuery.ajax({
           url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
           type: "GET",
           headers: { "Accept": "application/json;odata=verbose" }
        });
    
    }   
    
    
    function init() {
       this.compareCurrentUserWithListObject()
       .done(function(data) {
         var res = data.d.Email.indexOf(userName) > -1;
         if (res.length > 0) {
           //Do some stuff after compare...
         }
       });
    
    }
    

    it looks like you want to modify the response before using it in init. There is a way to do that but I'd do it inside the .done callback when using it.

    I didn't test this code so there might be mistakes. But the general answer is: you need to return the promise.

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt安装后运行不了,这是我电脑的问题吗
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法