У меня есть функция, которая в основном имеет 2, если условия на верхнем уровне. В if
и else
я говорю, чтобы он возвращал значение.
В зависимости от случая в функцию карты возвращаются разные значения.
Во время отладки я вижу, что возвращаемое значение 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' }....