[英]Nodejs display undefined on console even when function has return value
[英]Returns undefined even though return statement is present and variable has a value NodeJS
我有一个功能,如果在顶级条件下主要有2个。 在if
和else
我告诉它返回一个值。
根据具体情况,将不同的值返回给map函数。
在调试期间,我可以看到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 => {
用上面的函数开头替换,这将确保main函数永远不会为false并且仅通过过滤有效值来返回undefined
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.