Best answer

Output undefined in Zapier code step

  • 26 January 2021
  • 2 replies
  • 274 views

Hi all, I am trying to create a code step that does the following:

  1. Takes input data from a Shopify order
  2. Identifies which line items in the order are personalized
  3. Converts PDFs associated with these line items to JPG (via ConvertAPI)
  4. Submits a new order to the Printify API using the newly converted files

I keep getting an “output undefined” error in my code step. I have read that it’s possible to use “return” rather than “output”. I’d prefer to do this as I can’t quite work out how to get output to await the completion of an async function within my code step. If there’s a better way to ensure the entire code step is waiting until both async functions are completed, please let me know. Thank you in advance! (Sensitive data has been removed, and I am providing example inputData that doesn’t exist in the actual code)

 

//input data
var inputData = {
order_number : "1234",
shopify_id : "123456789",
line_item_name : "Personalized Item 1, Item 2, Personalized Item 3",
line_item_sku : "P-2-50-33728-ME01, 21212391028-12345, P-2-50-33726-ME01",
line_item_fulfillment : "manual, printify, manual",
line_item_properties : "12345,123456789-abcdefg123456789,https://www.google.com/,1,https://www.google.com/google.jpg,https://www.google.com/google.svg,2,12346,123456789-abcdefg123456790,https://www.google.com/,1,https://www.google.com/google.jpg,https://www.google.com/google.svg,2",
shipping_fname : "John",
shipping_lname : "Johnson",
shipping_email : "john@email.com",
shipping_phone : "800.555.5555",
shipping_country : "US",
shipping_region : "VA",
shipping_address1 : "1234 Main St",
shipping_address2 : "Apt 2",
shipping_city : "Richmond",
shipping_zip : "23223",
};

//prefixes
var personalized_prefix = "Prs-"
var non_personalized_prefix = "NonPrs-"
var custom_image_url_prefix = 'https://fpd-shopify-v2.herokuapp.com/api/fe/line_item_cache_print_pdf/'

//create arrays
var sku_array = inputData.line_item_sku.split(", ");
var properties_array = inputData.line_item_properties.split(",");

//count personalized items
var personalized_skus = sku_array.filter(function (sku) {
return sku[0].toLowerCase() === 'p';
});


//If there are no personalized items, end script
if (personalized_skus.length = 0) {
throw new Error("No Personalized Items");
}

//Begin Image Conversion
var image_url = new Array();

var requestOptions = {
method: 'POST',
redirect: 'follow'
};

personalized_skus.forEach(async(personalized_sku, index) => {
let pdf_url = custom_image_url_prefix.concat(properties_array[(7 * index) + 1]);
const response = await fetch(`httpsdo.convertapi.com//Pdf2Image?ApiKey=1234abcd5678efgh&File=${pdf_url}&ImageResolutionH=300&ImageResolutionV=300&JpgQuality=100&OutputFormat=jpg&StoreFile=true&Timeout=900`, requestOptions)
.then(response => console.log(response))
.then(result => image_url[index] = result)
.catch(error => console.log('error', error))
});

//build json for printify order
var printify_order = {};

//Shopify order ID and order number
printify_order.external_id = personalized_prefix.concat(inputData.shopify_id);
printify_order.label = personalized_prefix.concat(inputData.order_number);

//add personalized products
printify_order.line_items = new Array();
personalized_skus.forEach((personalized_sku, index) => {
let p_sku_split = personalized_sku.split("-");
printify_order.line_items[index] = {
print_provider_id : p_sku_split[1],
blueprint_id : p_sku_split[2],
variant_id : p_sku_split[3],
print_areas: {
front: image_url[index]
}
}
});

//Add shipping information
printify_order.shipping_method = 1;
printify_order.send_shipping_notification = false;
printify_order.address_to = {};
printify_order.address_to.first_name = inputData.shipping_fname;
printify_order.address_to.last_name = inputData.shipping_lname;
printify_order.address_to.email = inputData.shipping_email;
printify_order.address_to.phone = inputData.shipping_phone;
printify_order.address_to.country = inputData.shipping_country;
printify_order.address_to.region = inputData.shipping_region;
printify_order.address_to.address1 = inputData.shipping_address1;
printify_order.address_to.address2 = inputData.shipping_address2;
printify_order.address_to.city = inputData.shipping_city;
printify_order.address_to.zip = inputData.shipping_zip;

var json_order = JSON.stringify(printify_order);

var requestOptionsPrintify = {
method: 'POST',
headers: {
"Content-type" : "application/json",
"Authorization" : "Bearer abcd1234efgh5678"
},
body: json_order,
redirect: 'follow'
};

async function sendOrder() {const response = await fetch("https://api.printify.com/v1/shops/12345678/orders.json", requestOptionsPrintify)
.then(response => {
if (response.status !== 200) {
throw new Error(`Unexpected status code ${response.status}`);
} else {
return response;
}
}
)
}

 

icon

Best answer by GetUWired 26 January 2021, 15:02

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +12

Your error is because you haven’t defined any output in your code.

 It also looks like you’ve declared the function sendOrder() twice but are not calling it anywhere unless I’ve looked over it.
 

 

Userlevel 7
Badge +12

@jimbearings 

Just checking in to see if you still need help with this issue?