簡體   English   中英

即使存在return語句且變量具有值NodeJS,也返回undefined

[英]Returns undefined even though return statement is present and variable has a value NodeJS

我有一個功能,如果在頂級條件下主要有2個。 ifelse我告訴它返回一個值。

根據具體情況,將不同的值返回給map函數。

在調試期間,我可以看到if返回確實有一個我期望的值,但是當我記錄整個函數的結果時, if中的返回值是undefinedelse中的值正好通過。

可以在此處找到此功能的輸入文件: https//drive.google.com/file/d/1tBvav0HfGmPep9mbPLkJy6A5WivmWUro/view?usp=sharing

我一直在做這個了一整天,仍然不明白為什么在回else的工作,但在if沒有,即使它們是相似的

const fs = require('fs')
const _ = require("lodash")
let fixExtraCodeListsFinal = JSON.parse(fs.readFileSync("metadata.json").toString())

const test = fixExtraCodeListsFinal.map(fieldObj => {
    if (fieldObj.hasOwnProperty("relations")){
        fieldObjKeys = Object.keys(fieldObj.relations[0])
        let result = {}
        if (fieldObjKeys[0] == "0"){
            codeListDependencies = fieldObj.relations[0][0].codeList.allOf
           const codeListDependentList = codeListDependencies.map(codeListDependenciesObj => {
                if (codeListDependenciesObj.value.listItems){
                    resultDirty = {codeListDependentField: Object.keys(codeListDependenciesObj.attributes)[0], codeListItems: codeListDependenciesObj.value.listItems}
                    result = {codeListDependentField: getParentPath1(resultDirty.codeListDependentField).path, codeListItems: codeListDependenciesObj.value.listItems}
                    //console.log(result)
                }
                else  if (codeListDependenciesObj.value.links[0].href){
                    dependentPath = Object.keys(codeListDependenciesObj.attributes)[0]
                    result = {codeListDependentField: getParentPath1(dependentPath).path, relations: codeListDependenciesObj.value.links[0].href}
                    //THIS IS WHERE U STOPPED WRITING CODE
                    //console.log(result)
                    //console.log('idk what this case is dependencies')
                }
                else{
                    //console.log("unhandled metadata formatting, reach out to CE for a fix")
                }
                return result
        })
        //console.log(codeListDependentList)
        return codeListDependentList
        }
        else{
            fieldObjKeys = Object.keys(fieldObj.relations[0])
            fieldObjKeys.map(key => {
                if (fieldObj.relations[0][key].hasOwnProperty("allOf")){
                    dependentFieldsArr = fieldObj.relations[0][key].allOf
                    dependentFields = dependentFieldsArr.map(dependentFieldsArrObj => Object.keys(dependentFieldsArrObj.attributes))
                }
                else{
                    dependentFields = Object.keys(fieldObj.relations[0][key].oneOf.attributes)
                }
            })
            dependentPathADPFormat = _.flatten(dependentFields)
            dependentPathsCEformat = dependentPathADPFormat.map(getParentPath1)
            uniqueDependencyList = _.uniqBy(dependentPathsCEformat.map(obj => obj.path).map(dependentPath => fieldObj.conditionallyRequired = dependentPath))[0]

            originalFieldObj = fieldObj.vendorPath
            //console.log(JSON.stringify({uniqueDependencyList, originalFieldObj}))
            result = {uniqueDependencyList, originalFieldObj}
           return result

        }
        //this code needs to happen laters
        //fieldObj.conditionallyRequired = uniqueDependencyList[0]

        //console.log(JSON.stringify(fieldObj))
    }
    })
   console.log(test)

我在這里截斷了輸出。 這是當前的輸出

[ undefined,
  undefined,
  undefined,
  { uniqueDependencyList: 'worker.businessCommunication.faxes[*].countryDialing',
    originalFieldObj: 'businessCommunication.faxes[*].areaDialing' },
  undefined,
  { uniqueDependencyList: 'worker.businessCommunication.faxes[*].areaDialing',
    originalFieldObj: 'businessCommunication.faxes[*].dialNumber' },
  undefined,
  { uniqueDependencyList: 'worker.businessCommunication.landlines[*].countryDialing',
    originalFieldObj: 'businessCommunication.landlines[*].areaDialing' },
  undefined,
  { uniqueDependencyList: 'worker.businessCommunication.landlines[*].areaDialing',
    originalFieldObj: 'businessCommunication.landlines[*].dialNumber' },
  undefined,
  undefined,
  { uniqueDependencyList: 'worker.businessCommunication.mobiles[*].countryDialing',
    originalFieldObj: 'businessCommunication.mobiles[*].areaDialing' },
  undefined,
  { uniqueDependencyList: 'worker.businessCommunication.mobiles[*].areaDialing',
    originalFieldObj: 'businessCommunication.mobiles[*].dialNumber' },
  undefined,
  { uniqueDependencyList: 'worker.businessCommunication.pagers[*].countryDialing',
    originalFieldObj: 'businessCommunication.pagers[*].areaDialing' },
  undefined,
  { uniqueDependencyList: 'worker.businessCommunication.pagers[*].areaDialing',
    originalFieldObj: 'businessCommunication.pagers[*].dialNumber' },
  undefined,
  undefined,
  undefined,
  undefined.......

這是我的預期輸出也被截斷

{"uniqueDependencyList":"worker.person.governmentIDs[*].nameCode.codeValue.nameCode.codeValue","originalFieldObj":"person.governmentIDs[*].idValue"}
{"uniqueDependencyList":"worker.person.otherPersonalAddresses[*].countryCode","originalFieldObj":"person.otherPersonalAddresses[*].cityName"}
{"uniqueDependencyList":"worker.person.otherPersonalAddresses[*].countryCode","originalFieldObj":"person.otherPersonalAddresses[*].countrySubdivisionLevel1"}
{"uniqueDependencyList":"worker.person.otherPersonalAddresses[*].countryCode","originalFieldObj":""}
{"uniqueDependencyList":"worker.person.otherPersonalAddresses[*].countryCode","originalFieldObj":"person.otherPersonalAddresses[*].countrySubdivisionLevel1.shortName"}
{"uniqueDependencyList":"worker.person.otherPersonalAddresses[*].countryCode","originalFieldObj":"person.otherPersonalAddresses[*].lineOne"}
{"uniqueDependencyList":"worker.person.otherPersonalAddresses[*].countryCode","originalFieldObj":"person.otherPersonalAddresses[*].postalCode"}
{ codeListDependentField:
   'worker.workAssignment.homeOrganizationalUnits[*].typeCode.codeValue',
  relations:
   '/codelists/hr/v3/worker-management/departments/WFN/1?$filter=foreignKey eq {payrollGroupCode}' }
{ codeListDependentField:
   'worker.workAssignment.homeOrganizationalUnits[*].typeCode.codeValue',
  relations: '/codelists/hr/v3/worker-management/business-units/WFN/1' }
{ codeListDependentField:
   'worker.workAssignment.homeOrganizationalUnits[*].typeCode.codeValue',
  codeListItems:
   [ { codeValue: '00-1005-FRM-PA', shortName: 'Non Cert Job' },
     { codeValue: '001000201', shortName: 'NON Cert job' },
     { codeValue: '001101000', shortName: '001101000' },
     { codeValue: '001101AA', shortName: '001101AA' },
     { codeValue: '001101DM', shortName: '001101DM' },
     { codeValue: '001101MA', shortName: '001101MA' }....
fixExtraCodeListsFinal1 = fixExtraCodeListsFinal.filter(fieldObj => fieldObj.hasOwnProperty("relations"))
const dependenciesResultToHydrate = fixExtraCodeListsFinal1.map(fieldObj => {

用上面的函數開頭替換,這將確保main函數永遠不會為false並且僅通過過濾有效值來返回undefined

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM