fix: Do not join field list of the field doesn't exists

This commit is contained in:
Josepablo C
2024-04-01 21:49:43 -06:00
parent 027f5f4d34
commit 3c650138b9
2 changed files with 8 additions and 4 deletions

View File

@@ -22,7 +22,9 @@ function join_field_list( obj_with_fields , list_of_fields )
for(let field_idx=0; field_idx < list_of_fields.length; field_idx++){
const field_name = list_of_fields[ field_idx ];
const new_field_name = "_" + list_of_fields[ field_idx ];
obj_with_fields[ new_field_name ] = obj_with_fields[field_name].join(", ");
if( obj_with_fields[ field_name ] ){
obj_with_fields[ new_field_name ] = obj_with_fields[field_name].join(", ");
}
}
return obj_with_fields;
}
@@ -195,7 +197,7 @@ const getUserLists = async(req, res) => {
data_list[i].name = name;
data_list[i] = join_field_list( data_list[i] , ["categories","user_city","user_state","truck_type"] );
let categories = data_list[i].categories.map( ( c ) => c.name);
// let categories = data_list[i].categories.map( ( c ) => c.name);
/** Remove not requried fields */
delete data_list[i].categories;
@@ -212,7 +214,7 @@ const getUserLists = async(req, res) => {
return res.status(200).send( retVal );
} catch ( err ){
console.error( err );
return res.status(500).send({ error : "Public-Companies: Internal error" });
return res.status(500).send({ error : "Companies: Internal error" });
}
};

View File

@@ -21,7 +21,9 @@ function join_field_list( obj_with_fields , list_of_fields )
for(let field_idx=0; field_idx < list_of_fields.length; field_idx++){
const field_name = list_of_fields[ field_idx ];
const new_field_name = "_" + list_of_fields[ field_idx ];
obj_with_fields[ new_field_name ] = obj_with_fields[field_name].join(", ");
if( obj_with_fields[ field_name ] ){
obj_with_fields[ new_field_name ] = obj_with_fields[field_name].join(", ");
}
}
return obj_with_fields;
}