2017-12-18 3 views
0

ich auf diese Methode den obigen Fehler haben:Kann nicht Eigentum ‚und dann‘ undefinierter in Versprechen lesen

function addContentType(listItem){ 
       var promise = getContentTypeOfCurrentItem(listItem.ID.toString()); 
       promise.then(function(cname){ 
        listItem['Document Type'] = cname; //we add the doc type to each listItem, not only the last one 
       }); 
       return promise; 
      } 

Entire-Code ist hier:

function GetRelatedBillingDocumentsFromList(selectProperties, currentBillCyclePath, clientCode, jobCodes, engagementCode, enhanceFunctions) { 
       $log.info("Retrieving related billing documents for bill cycle with name [" + currentBillCyclePath + "]");     
       var deferred = $q.defer(); 
       var webUrl = _spPageContextInfo.webAbsoluteUrl; 
       var viewFields = spService.ConvertSelectPropertiesToViewFields(selectProperties); 
       // query must return the documents for the same client but in other bill cycles not the current one 
       var camlQuery = '<View Scope="RecursiveAll">' + viewFields + 
         '<Query>' + 
          '<Where>' + 
           '<And>' + 
            '<Eq>' + 
             '<FieldRef Name="ClientCode" />' + 
             '<Value Type="Text">'+ clientCode + '</Value>' + 
            '</Eq>' + 
            '<Neq>' + 
             '<FieldRef Name="ContentType" />' + 
             '<Value Type="Computed">Bill Cycle</Value>' + 
            '</Neq>' + 
           '</And>' + 
          '</Where>' + 
         '</Query>' + 
        '</View>'; 

       var billCyclesListId = "{c23bbae4-34f7-494c-8f67-acece3ba60da}";      
       spService.GetListItems(billCyclesListId, camlQuery, selectProperties) 
       .then(function(listItems) {     
        var listItemsWithValues = []; 

        if(listItems) { 
         var enumerator = listItems.getEnumerator(); 
         var promises = []; 
         while (enumerator.moveNext()) { 
          var listItem = enumerator.get_current(); 
          var listItemValues = [];         
          selectProperties 
          .forEach(function(propertyName) {        
           var value = listItem.get_item(propertyName); 
           if(propertyName === "PwC_JobCodesMulti"){ 
            jobvalue = ""; 
            value.forEach(function(jobvalues){ 
             jobvalue+= jobvalues.get_lookupValue() +";"; 
            }) 
            listItemValues[propertyName] = jobvalue; 
           }else{ 
            listItemValues[propertyName] = value; 
           } 
          }); 

          listItemsWithValues.push(listItemValues); 
         } 

         var promises = listItemsWithValues.map(addContentType); 
         Promise.all(promises).then(youCanUseTheData); 

         function youCanUseTheData(){ 
          /* 
          At this point, each listItem holds the 'Document Type' info 
          */ 
          listItemsWithValues.forEach(function(listItem) { 
           var fileDirRef = listItem["FileRef"]; 
           var id = listItem["ID"]; 
           var title = listItem["Title"]; 
           var serverUrl = _spPageContextInfo.webAbsoluteUrl.replace(_spPageContextInfo.webServerRelativeUrl,"");       
           var dispFormUrl = serverUrl + "/sites/billing/_layouts/15/DocSetHome.aspx?id="+fileDirRef; 
           //listItem["FileRef"] = dispFormUrl; 
           //listItem["Bill Cycle"] = dispFormUrl; 

           var parentLink = listItem["FileRef"]; 
           arrayofstrings = parentLink.split("/"); 
           var billCycleFolderName = arrayofstrings[arrayofstrings.length-2]; 
           arrayofstrings.pop(); 
           var hyperLink = '<a href="' + arrayofstrings.join('/') + '">' + billCycleFolderName + '</a>';       
           listItem["Bill Cycle"] = hyperLink; 

          }); 
         } 
        }     


        var enhancedListItemValues = spService.SpSearchQuery.EnhanceSearchResults(listItemsWithValues, enhanceFunctions);      
        deferred.resolve(listItemsWithValues); 
       }) 
       .catch (function (message) { 
        deferred.reject(); 
       }); 

       return deferred.promise; 
      } 

      function addContentType(listItem){ 
       var promise = getContentTypeOfCurrentItem(listItem.ID.toString()); 
       promise.then(function(cname){ 
        listItem['Document Type'] = cname; //we add the doc type to each listItem, not only the last one 
       }); 
       return promise; 
      } 

      function getContentTypeOfCurrentItem(id) {    
       var clientContext = new SP.ClientContext.get_current(); 
       var oList = clientContext.get_web().get_lists().getByTitle("Bill Cycles"); 
       listItem = oList.getItemById(id); 
       clientContext.load(listItem); 
       listContentTypes = oList.get_contentTypes(); 
       clientContext.load(listContentTypes); 
       clientContext.executeQueryAsync(
        function() { 
         $log.info("Successfully retrieved getContentTypeOfCurrentItemt"); 
         var ctid = listItem.get_item("ContentTypeId").toString();    
         var ct_enumerator = listContentTypes.getEnumerator(); 
         while (ct_enumerator.moveNext()) { 
          var ct = ct_enumerator.get_current();    
          if (ct.get_id().toString() == ctid) { 
           var contentTypeName = ct.get_name(); 
          } 
         } 
         return Promise.resolve(contentTypeName); 
        }, 
        function(error, errorInfo) { 
         $log.warn("Retrieving getContentTypeOfCurrentItem failed"); 
         deferred.reject(errorInfo); 
        } 
       ); 
      } 

Nicht sicher genau das, was ich fehle

Update 1:

Ich änderte den Code als Niels antwortete tere, aber dann bekomme ich den Fehler unten:

Uncaught Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. 

Wenn ich die Änderungen vorgenommen zu analysieren, das einzige, was auf der Methode geändert ist die Rückkehr neue Versprechen, der Rest ist das gleiche und es wurde vor der Arbeit. Ich habe nicht alle Änderungen vorgenommen, wie es Niels getan hat, ich muss es nur ohne viel Aufwand machen. :)

function GetRelatedBillingDocumentsFromList(selectProperties, currentBillCyclePath, clientCode, jobCodes, engagementCode, enhanceFunctions) { 
       $log.info("Retrieving related billing documents for bill cycle with name [" + currentBillCyclePath + "]");     
       var deferred = $q.defer(); 
       var webUrl = _spPageContextInfo.webAbsoluteUrl; 
       var viewFields = spService.ConvertSelectPropertiesToViewFields(selectProperties); 
       // query must return the documents for the same client but in other bill cycles not the current one 
       var camlQuery = '<View Scope="RecursiveAll">' + viewFields + 
         '<Query>' + 
          '<Where>' + 
           '<And>' + 
            '<Eq>' + 
             '<FieldRef Name="ClientCode" />' + 
             '<Value Type="Text">'+ clientCode + '</Value>' + 
            '</Eq>' + 
            '<Neq>' + 
             '<FieldRef Name="ContentType" />' + 
             '<Value Type="Computed">Bill Cycle</Value>' + 
            '</Neq>' + 
           '</And>' + 
          '</Where>' + 
         '</Query>' + 
        '</View>'; 

       var billCyclesListId = "{c23bbae4-34f7-494c-8f67-acece3ba60da}";      
       spService.GetListItems(billCyclesListId, camlQuery, selectProperties) 
       .then(function(listItems) {     
        var listItemsWithValues = []; 

        if(listItems) { 
         var enumerator = listItems.getEnumerator(); 
         var promises = []; 
         while (enumerator.moveNext()) { 
          var listItem = enumerator.get_current(); 
          var listItemValues = [];         
          selectProperties 
          .forEach(function(propertyName) {        
           var value = listItem.get_item(propertyName); 
           if(propertyName === "PwC_JobCodesMulti"){ 
            jobvalue = ""; 
            value.forEach(function(jobvalues){ 
             jobvalue+= jobvalues.get_lookupValue() +";"; 
            }) 
            listItemValues[propertyName] = jobvalue; 
           }else{ 
            listItemValues[propertyName] = value; 
           } 
          }); 

          listItemsWithValues.push(listItemValues); 
         } 

         var promises = listItemsWithValues.map(addContentType); 
         Promise.all(promises).then(youCanUseTheData); 

         function youCanUseTheData(){ 
          /* 
          At this point, each listItem holds the 'Document Type' info 
          */ 
          listItemsWithValues.forEach(function(listItem) { 
           var fileDirRef = listItem["FileRef"]; 
           var id = listItem["ID"]; 
           var title = listItem["Title"]; 
           var serverUrl = _spPageContextInfo.webAbsoluteUrl.replace(_spPageContextInfo.webServerRelativeUrl,"");       
           var dispFormUrl = serverUrl + "/sites/billing/_layouts/15/DocSetHome.aspx?id="+fileDirRef; 
           //listItem["FileRef"] = dispFormUrl; 
           //listItem["Bill Cycle"] = dispFormUrl; 

           var parentLink = listItem["FileRef"]; 
           arrayofstrings = parentLink.split("/"); 
           var billCycleFolderName = arrayofstrings[arrayofstrings.length-2]; 
           arrayofstrings.pop(); 
           var hyperLink = '<a href="' + arrayofstrings.join('/') + '">' + billCycleFolderName + '</a>';       
           listItem["Bill Cycle"] = hyperLink; 

          }); 
         } 
        }     


        var enhancedListItemValues = spService.SpSearchQuery.EnhanceSearchResults(listItemsWithValues, enhanceFunctions);      
        deferred.resolve(listItemsWithValues); 
       }) 
       .catch (function (message) { 
        deferred.reject(); 
       }); 

       return deferred.promise; 
      } 

      function addContentType(listItem){ 
       return getContentTypeOfCurrentItem(listItem.ID.toString()).then(function(cname){ 
        listItem['Document Type'] = cname; //we add the doc type to each listItem, not only the last one 
       }); 
      } 

      function getContentTypeOfCurrentItem(id) { 
       return new Promise(function (resolve, reject) { 
        var clientContext = new SP.ClientContext.get_current(); 
        var oList = clientContext.get_web().get_lists().getByTitle("Bill Cycles"); 
        listItem = oList.getItemById(id); 
        clientContext.load(listItem); 
        listContentTypes = oList.get_contentTypes(); 
        clientContext.load(listContentTypes); 
        clientContext.executeQueryAsync(
         function() { 
          $log.info("Successfully retrieved getContentTypeOfCurrentItemt"); 
          var ctid = listItem.get_item("ContentTypeId").toString();    
          var ct_enumerator = listContentTypes.getEnumerator(); 
          while (ct_enumerator.moveNext()) { 
           var ct = ct_enumerator.get_current();    
           if (ct.get_id().toString() == ctid) { 
            var contentTypeName = ct.get_name(); 
           } 
          } 
          resolve(contentTypeName); 
         }, 
         function(error, errorInfo) { 
          $log.warn("Retrieving getContentTypeOfCurrentItem failed"); 
          reject(errorInfo); 
         } 
        ); 
       }); 
      } 
+2

Die 'getContentTypeOfCurrentItem' Funktion nichts zurückgibt – user184994

+0

@ luis https://StackOverflow.com/a/42577762/713789 – Adrian

Antwort

1

getContentTypeOfCurrentItem sollte ein Versprechen zurück. Unter der Annahme, dass clientContext.executeQueryAsync nicht ein Versprechen nicht zurückgibt, da es mit Handler:

function getContentTypeOfCurrentItem(id) { 
    return new Promise(function (resolve, reject) { 
     var clientContext = new SP.ClientContext.get_current(); 
     var oList = clientContext.get_web().get_lists().getByTitle("Bill 
Cycles"); 
     listItem = oList.getItemById(id); 
     clientContext.load(listItem); 
     listContentTypes = oList.get_contentTypes(); 
     clientContext.load(listContentTypes); 
     clientContext.executeQueryAsync(
      function() { 
       $log.info("Successfully retrieved 
getContentTypeOfCurrentItemt"); 
       var ctid = listItem.get_item("ContentTypeId").toString();    
       var ct_enumerator = listContentTypes.getEnumerator(); 
       while (ct_enumerator.moveNext()) { 
        var ct = ct_enumerator.get_current();    
        if (ct.get_id().toString() == ctid) { 
         var contentTypeName = ct.get_name(); 
        } 
       } 
       resolve(contentTypeName); 
      }, 
      function(error, errorInfo) { 
       $log.warn("Retrieving getContentTypeOfCurrentItem failed"); 
       reject(errorInfo); 
      } 
     ); 
    }); 
} 

addContentType kann auch einfacher sein:

function addContentType(listItem){ 
    return getContentTypeOfCurrentItem(listItem.ID.toString()).then(function(cname) { 
     listItem['Document Type'] = cname; //we add the doc type to each listItem, not only the last one 
    }).catch(function(error) { 
     $log.warn("Server error"); 
    }); 
} 
+0

Bitte beachten Sie mein Update, wie ich diesen Fehler –

+0

@LuisValencia Sieht aus wie Sie einen Schritt nach vorn sind. Dies scheint ein Serverfehler zu sein. Ich habe einen Fang in meinem addContentType-Teil hinzugefügt. Sie können auch die Serverantwort von Fiddler oder Browser (Chrome) Inspector überprüfen. –

1

Die Funktion getContentTypeOfCurrentItem gibt nichts zurück. Ändern Sie es einfach Ihre Versprechen zurückzukehren (vorausgesetzt natürlich, dass clientContext.executeQueryAsync ein Versprechen zurückgibt):

function getContentTypeOfCurrentItem(id) { 
    var clientContext = new SP.ClientContext.get_current(); 
    var oList = clientContext.get_web().get_lists().getByTitle("Bill Cycles"); 
    listItem = oList.getItemById(id); 
    clientContext.load(listItem); 
    listContentTypes = oList.get_contentTypes(); 
    clientContext.load(listContentTypes); 
    return clientContext.executeQueryAsync(
    function() { 
     $log.info("Successfully retrieved getContentTypeOfCurrentItemt"); 
     var ctid = listItem.get_item("ContentTypeId").toString(); 
     var ct_enumerator = listContentTypes.getEnumerator(); 
     while (ct_enumerator.moveNext()) { 
     var ct = ct_enumerator.get_current(); 
     if (ct.get_id().toString() == ctid) { 
      var contentTypeName = ct.get_name(); 
     } 
     } 
     return Promise.resolve(contentTypeName); 
    }, 
    function(error, errorInfo) { 
     $log.warn("Retrieving getContentTypeOfCurrentItem failed"); 
     deferred.reject(errorInfo); 
    } 
); 
} 
Verwandte Themen