无法读取承诺中未定义的属性“then”

路易斯瓦伦西亚

我在此方法上有上述错误:

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 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);
                   }
                );
            }

不确定我缺少什么

更新 1:

我在 Niels 回答 tere 时更改了代码,但随后出现以下错误:

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.

如果我分析所做的更改,方法上唯一改变的是返回新的承诺,其余的都是一样的,它以前是有效的。我没有像 Niels 那样做所有的改变,只需要不费吹灰之力就可以完成这项工作。:)

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);
                       }
                    );
                });
            }
尼尔斯·斯汀贝克

getContentTypeOfCurrentItem 应该返回一个 Promise。假设 clientContext.executeQueryAsync 不返回承诺,因为它正在使用处理程序:

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 也可以更简单:

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");
    });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

角嵌套承诺'无法读取未定义的属性'then'

来自分类Dev

模拟服务返回承诺:无法读取未定义的属性

来自分类Dev

v-on处理程序中的错误(承诺/异步):“ TypeError:无法读取未定义的属性'data'” //未定义

来自分类Dev

使用人脸api的javascript中的人脸识别“未捕获(承诺)TypeError:无法读取未定义的属性'descriptor'”

来自分类Dev

MetaMask不注入window.ethereum:未捕获(在承诺中)TypeError:无法读取未定义的属性“ request”

来自分类常见问题

React无法读取if块中未定义的属性“ setState”

来自分类Dev

无法读取Angular Mobile中未定义的属性“ makeCurrent”

来自分类Dev

无法读取Google图表中未定义的属性“计数”

来自分类Dev

无法读取angularJS中未定义的属性“替换”?

来自分类Dev

无法读取角度2中未定义的属性“错误”

来自分类Dev

无法读取Angular JS中未定义的属性成功

来自分类Dev

TypeError:无法从未定义中读取属性“ 1”

来自分类Dev

无法从未定义中读取属性“纹理”

来自分类Dev

无法读取DocumentDB中未定义的属性“ _self”

来自分类Dev

TypeError:无法读取[null]中未定义的属性“ post”

来自分类Dev

TypeScript:无法读取[null]中未定义的属性“ push”

来自分类Dev

无法读取Dojo中未定义的属性“ set”

来自分类Dev

无法读取角度2中未定义的属性“ get”

来自分类Dev

无法读取JavaScript错误中未定义的属性'preventDefault'

来自分类Dev

无法读取ngAfterViewInit中未定义的属性“ createComponent”

来自分类Dev

TypeError:无法读取JavaScript中未定义的属性“ push”

来自分类Dev

无法读取NgbTypeahead中未定义的属性“ pipe”

来自分类Dev

无法读取Jodit中未定义的属性“ fire”

来自分类Dev

无法读取功能组件中未定义的属性“ props”

来自分类Dev

TypeError:无法读取ReactJS中未定义的属性'focus'

来自分类Dev

无法读取BoostrapVue Modal中未定义的属性“ show”

来自分类Dev

TypeError:无法读取MERN中未定义的属性“发送”

来自分类Dev

无法在react中读取未定义的属性“ someProperty”

来自分类Dev

无法读取nodejs中未定义的属性“ body”

Related 相关文章

  1. 1

    角嵌套承诺'无法读取未定义的属性'then'

  2. 2

    模拟服务返回承诺:无法读取未定义的属性

  3. 3

    v-on处理程序中的错误(承诺/异步):“ TypeError:无法读取未定义的属性'data'” //未定义

  4. 4

    使用人脸api的javascript中的人脸识别“未捕获(承诺)TypeError:无法读取未定义的属性'descriptor'”

  5. 5

    MetaMask不注入window.ethereum:未捕获(在承诺中)TypeError:无法读取未定义的属性“ request”

  6. 6

    React无法读取if块中未定义的属性“ setState”

  7. 7

    无法读取Angular Mobile中未定义的属性“ makeCurrent”

  8. 8

    无法读取Google图表中未定义的属性“计数”

  9. 9

    无法读取angularJS中未定义的属性“替换”?

  10. 10

    无法读取角度2中未定义的属性“错误”

  11. 11

    无法读取Angular JS中未定义的属性成功

  12. 12

    TypeError:无法从未定义中读取属性“ 1”

  13. 13

    无法从未定义中读取属性“纹理”

  14. 14

    无法读取DocumentDB中未定义的属性“ _self”

  15. 15

    TypeError:无法读取[null]中未定义的属性“ post”

  16. 16

    TypeScript:无法读取[null]中未定义的属性“ push”

  17. 17

    无法读取Dojo中未定义的属性“ set”

  18. 18

    无法读取角度2中未定义的属性“ get”

  19. 19

    无法读取JavaScript错误中未定义的属性'preventDefault'

  20. 20

    无法读取ngAfterViewInit中未定义的属性“ createComponent”

  21. 21

    TypeError:无法读取JavaScript中未定义的属性“ push”

  22. 22

    无法读取NgbTypeahead中未定义的属性“ pipe”

  23. 23

    无法读取Jodit中未定义的属性“ fire”

  24. 24

    无法读取功能组件中未定义的属性“ props”

  25. 25

    TypeError:无法读取ReactJS中未定义的属性'focus'

  26. 26

    无法读取BoostrapVue Modal中未定义的属性“ show”

  27. 27

    TypeError:无法读取MERN中未定义的属性“发送”

  28. 28

    无法在react中读取未定义的属性“ someProperty”

  29. 29

    无法读取nodejs中未定义的属性“ body”

热门标签

归档