ActiveCampaign / activecampaign-api-nodejs

Node.js wrapper for the ActiveCampaign API
MIT License
39 stars 36 forks source link

Error Handing #3

Closed selipso closed 10 years ago

selipso commented 10 years ago

I wrote a code snippet that transfers a contact from one list to the other if they exist in the system. If they don't exist, it creates a new contact and adds them to the system. This code snippet doesn't work though.

var contact_exists = ac.api("contact/view?email=test@example.org", {}, function(response){
  var contact;
  if(response){
    if(response.lists[30]){     //If subscriner is already subscribed, does nothing
      return;
    }
    contact = {
      email: response.email,
      'p[123]': 30,                                       //adds subscriber to list
      'status[123]': 1,
    };
    if(response.first_name){  
      contact.first_name = response.first_name;
    }
    if(response.last_name){
      contact.last_name = response.last_name;
    }
    if(response.fields[4]){
      contact.fields[28, 0] = response.fields[4].val;     //adds field name if available
    }

    var contact_delete = ac.api('contact/delete', {'id': response.id}, function(nestedresponse){
      console.log(nestedresponse);
    });
  }

// If contact is not in the list, the following blocks of code should still execute, but they don't  

  contact.email = "test@example.org";
  contact.p[123] = 30;
  contact.status[123] = 1;

  // Add the contact to the subscribed list

  var contact_add = ac.api("contact/add", contact, function(response){
    console.log(response);
  });
});
selipso commented 10 years ago

I read the post on Detecting API Errors, but I am not receiving the status codes through the Node.js wrapper.

var contact_exists = ac.api("contact/view?email=test@example.org", {}, function(response) {
  console.log(Object.keys(response));
  console.log(response.result_code);
  console.log(response.result_message);
  console.log(response.result_output);
});

The above code simply returns Error:404