返回未定义,即使返回语句存在并且变量具有值 NodeJS



>我有一个函数,它主要在顶级有 2 个 if 条件。在ifelse中,我告诉它返回一个值。

根据具体情况,不同的值将返回给映射函数。

在调试过程中,我可以看到if返回确实具有我期望的值,但是当我记录整个函数的结果时,if返回值的结果是undefined,而else中的值则正常。

此函数的输入文件可在此处找到: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 => {

将函数的开头替换为上面,这将确保主函数永远不会为 false,并且仅通过首先过滤有效值来返回 undefined。

最新更新