如果评估不正常

时间:2018-05-28 11:06:00

标签: javascript

我有一个if语句,当我在控制台中打印结果时,我有时会看到它是真的,有时它是假的。

然而,IF中的内容是什么,它从未执行过,结果数组总是空的。

var createQuery = function(viewFields,clientCode) {
                return '<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 createListItemValues = function(filter) {
                return function(listItems,selectProperties) {
                  var listItemsWithValues = [];
                  if (listItems) {
                    var enumerator = listItems.getEnumerator();
                    while (enumerator.moveNext()) {
                      var listItem = enumerator.get_current();
                      var listItemValues = [];

                      selectProperties
                        .forEach(function (propertyName) {
                          var value = listItem.get_item(propertyName);
                          if (propertyName === "JobCodesMulti") {
                            jobvalue = "";
                            value.forEach(function (jobvalues) {
                              jobvalue += jobvalues.get_lookupValue() + ";";
                            })
                            listItemValues[propertyName] = jobvalue;
                          } else {
                            listItemValues[propertyName] = value;
                          }
                        });
                      if(filter(listItemValues)){//only push if filter returns true
                        listItemsWithValues.push(listItemValues);
                      }
                    }
                  }
                  return listItemsWithValues;
                };
              };
              var processListItemWithValue = function(listItemsWithValues) {
                return 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;
                  var parentLink = listItem["FileRef"];
                  //!!!PLEASE NOTE: made arrayofstrings a local variable
                  var arrayofstrings = parentLink.split("/");
                  var billCycleFolderName = arrayofstrings[arrayofstrings.length - 2];
                  arrayofstrings.pop();
                  var hyperLink = '<a href="' + arrayofstrings.join('/') + '">' + billCycleFolderName + '</a>';
                  listItem["Bill Cycle"] = hyperLink;
                  listItemsWithValues["Document Type"] = getContentTypeOfCurrentItem(listItem.ID.toString());
                }
              };

              function GetRelatedBillingDocumentsFromList(selectProperties, currentBillCyclePath, clientCode, jobCodes, engagementCode, enhanceFunctions) {
                $log.info("Retrieving related billing documents for bill cycle with name [" + currentBillCyclePath + "]");
                //pass filter function to createListItemValues to get a new function that
                //  creates filtered list item values 
                var createFilteredListItemsWithValues = createListItemValues(
                  function(listItemValues) {

                    var x1=listItemValues && typeof listItemValues.FileRef === "string" && listItemValues.FileRef.split("/")[4];
                    var x2= currentBillCyclePath.split("/")[8]
                    console.log(x1===x2);


                    return !(//pass filter function to createListItemValues
                      listItemValues && 
                      typeof listItemValues.FileRef === "string" &&
                      listItemValues.FileRef.split("/")[4]
                    ) === currentBillCyclePath.split("/")[8];
                  }
                );
                var webUrl = _spPageContextInfo.webAbsoluteUrl;
                selectProperties = selectProperties.concat("ContentTypeId");
                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 = createQuery(viewFields,clientCode);
                var billCyclesListId = "{c23bbae4-34f7-494c-8f67-acece3ba60da}";
                //return a promise like here so the caller knows if something went wrong
                return spService.GetListItems(billCyclesListId, camlQuery, selectProperties)
                .then(
                  function(listItems){
                    console.log("currentBillCyclePath:",currentBillCyclePath);
                    var listItemsValues = createFilteredListItemsWithValues
                      (listItems,selectProperties);
                    return $q.all(listItemsValues.map(addContentType))
                    .then(function(){ return listItemsValues; })//finished asynchronously mutating array of listItems
                  }
                ).then(
                  function(listItemsWithValues) {
                    listItemsWithValues.forEach(processListItemWithValue(listItemsWithValues));
                    return $q.all(
                      spService.SpSearchQuery.EnhanceSearchResults(listItemsWithValues, enhanceFunctions)
                    )
                  }
                )
              }

重要的行是:var createFilteredListItemsWithValuesif(filter(listItemValues))

enter image description here

1 个答案:

答案 0 :(得分:1)

你的过滤函数总是返回false,因为你正在检查一个String值是否等于一个布尔值。

!(
  listItemValues &&
  typeof listItemValues.FileRef === "string" &&
  listItemValues.FileRef.split("/")[4]
)

是布尔值,而

currentBillCyclePath.split("/")[8];

是一个字符串。