Skip to content
Advertisement

Using an empty string in path fails

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.

  if(result.content._type === "farm") {
      emailPath = 'Record'
      emailPathFull = 'Record.emails'

    } else {
      emailPath = ''
      emailPathFull = 'emails'
    }
    
   var e_emails = result.content[emailPath].emails;

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

 if(result.content._type === "farm"){
     e_emails = result.content.Record.emails;}
  else {
     e_emails = result.content.emails;}

Advertisement

Answer

You can’t do it like that, and more so, you don’t need too.

  1. Define a variable that will receive the value of emails.
  2. Use the conditional to disambiguate the input
  3. Copy the emails from where it resides in the input, based on type
let e_emails

if(result.content._type === "farm") {
  e_emails = result.content.Record.emails
} else {
  e_emails = result.content.emails
}
    
console.log(e_emails)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement