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 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题