简体   繁体   中英

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

I have a function that primarily has 2 if conditions at the top level. In the if and the else I tell it to return a value.

Depending on the case different values are returned to the map function.

During debugging I can see that the if return does have a value I am expecting but when I log the result of the entire function return values from the if are undefined while values in the else are coming through fine.

The input file for this function can be found here: https://drive.google.com/file/d/1tBvav0HfGmPep9mbPLkJy6A5WivmWUro/view?usp=sharing

I have been working on this for a whole day and still do not understand why the return for the else does work but the if does not even though they are similar

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)

I have truncated the output here. here is the current output

[ 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.......

Here is my expected output also truncated

{"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 => {

Replace the beginning of the function with above, this will ensure that the main function is never false and returning undefined by filtering for valid values only first

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM