In my node API i have some function that updates the email address Array of either a contact or a farm, its the same concept but the difference is where the array is located in farms is in Records.emails and in Contacts its in emails. So in my case I decide based on _type what the path is.
JavaScript
x
11
11
1
if(result.content._type === "farm") {
2
emailPath = 'Record'
3
emailPathFull = 'Record.emails'
4
5
} else {
6
emailPath = ''
7
emailPathFull = 'emails'
8
}
9
10
var e_emails = result.content[emailPath].emails;
11
In case the emailPath is ” which happens to be the case for Contact it cant get the data. So i am wondering if there is a way to make this work other then using
JavaScript
1
5
1
if(result.content._type === "farm"){
2
e_emails = result.content.Record.emails;}
3
else {
4
e_emails = result.content.emails;}
5
Advertisement
Answer
You can’t do it like that, and more so, you don’t need too.
- Define a variable that will receive the value of emails.
- Use the conditional to disambiguate the input
- Copy the emails from where it resides in the input, based on type
JavaScript
1
10
10
1
let e_emails
2
3
if(result.content._type === "farm") {
4
e_emails = result.content.Record.emails
5
} else {
6
e_emails = result.content.emails
7
}
8
9
console.log(e_emails)
10