Question

multiple fetch request

  • 18 February 2022
  • 4 replies
  • 330 views

Userlevel 2
Badge

Hi, i'm trying to make a script that has multiple fetch request in it, for Exact online.

the script works perfectly fine when i run it localy on my computer but  when I try to run it into zapier i get the error 

string: You did not define `output`! Try `output = {id: 1, hello: await Promise.resolve("world")};` site:stackoverflow.com 

 

i've tried multiple things to make it work with zapier but i seems like zapier stops running the script just before the first fetch.

 

is there something i’m not seeing or is there a limitation of zapier?

 

my code:

 

var token = 'token';
var divisionid = 'divisionid';

var AMRelatieData = {
"Name": "company name",
"City": "city name",
"Website": "website.com"
};


var AMContactData = {
"FirstName": "firstname",
"LastName": "lastname",
"City": "name city"
};
var testrlid;

async function actionHandeler(actionValue) {

var action = actionValue;
if (action == "cp_maken_&_relatie_maken") {


var maakRelatieWaarde = maakRelatie(AMRelatieData).then(response => {
var POSTrelatieID = response;

AMContactData.Account = POSTrelatieID;

var maakContactwaarde = maakContact(AMContactData).then(response => {
var POSTcontactID = response;

testcpid = POSTcontactID;
testrlid = POSTrelatieID;

return ('maakContactwaarde succes');
});

return (maakContactwaarde);
});

return (maakRelatieWaarde);
}

//functions
async function updateRelatie(updateData, relatieId) {

var UpdateRelatiePUT = await PUTreq(1, updateData, relatieId);

console.log(UpdateRelatiePUT);

return ("updateRelatie succes");
}

async function maakRelatie(createData) {
var relatieId;

console.log('maakRelatie: ');



var maakRelatiePOST = await POSTreq(1, createData);

console.log('maakRelatieFunc:' + JSON.stringify(maakRelatiePOST));

return await maakRelatiePOST.d.ID;
}

async function maakContact(createData) {
var contactId;


var maaktcontactPOST = await POSTreq(2, createData);

console.log('maaktcontactFunc:' + JSON.stringify(maaktcontactPOST));


var jsonData = {
MainContact: maaktcontactPOST.d.ID
};

var relatieIdUpdate = createData.Account;
await updateRelatie(jsonData, relatieIdUpdate);
}

async function POSTreq(type, DATA) {
console.log('postreq');

var POSTendpoint = 'https://start.exactonline.nl/api/v1/'+ divisionid +'/crm/';
if (type == 1) {

POSTendpoint += 'Accounts';
}
if (type == 2) {

POSTendpoint += 'Contacts';
}

var outputPOST;
console.log(DATA);
await fetch(POSTendpoint, {
method: "POST",
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
},
body: JSON.stringify(DATA)
}).then(response => {
return response.json();
}).then(jsonResponse => {
var responseOut = jsonResponse;
outputPOST = responseOut;
}).catch(error => {
console.log(error);
});
return outputPOST;
}

async function PUTreq(type, DATA, id) {
var PUTendpoint = 'https://start.exactonline.nl/api/v1/'+ divisionid +'/crm/';

console.log('put data');
console.log(id);
console.log('data' + DATA);
console.log(type);

if (type == 1) {
PUTendpoint += "Accounts(guid'" + id + "')";
}
if (type == 2) {
PUTendpoint += "Contacts(guid'" + id + "')";
}

console.log(PUTendpoint);
console.log(PUTendpoint);
await fetch(PUTendpoint, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify(DATA)
});
}
}


async function actionHandlerFunc(){
console.log("begin");
await actionHandeler("cp_maken_&_relatie_maken");

return ("done did sum stuff");
};

actionHandlerFunc().then(result =>{
console.log('====================================');
console.log(result);
console.log('====================================');
});

 


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

4 replies

Userlevel 7
Badge +12

Hi @Jeff Thorns 

I looked at your first function and believe that some of those variables may still be promises when Zapier tries to return them… 
 

async function actionHandeler(actionValue) {

var action = actionValue;
if (action == "cp_maken_&_relatie_maken") {


var maakRelatieWaarde = maakRelatie(AMRelatieData).then(response => { //i believe this variable is a promise try just a return statement instead
var POSTrelatieID = response;

AMContactData.Account = POSTrelatieID;

var maakContactwaarde = maakContact(AMContactData).then(response => { //this variable is a promise, try just a return statement instead
var POSTcontactID = response;

testcpid = POSTcontactID;
testrlid = POSTrelatieID;

return ('maakContactwaarde succes');
});

return (maakContactwaarde); //this is an unresolved promise. you can use return in the above and it wont be needed
});

return (maakRelatieWaarde); //this is an unresolved promise. you can use return in the above and it wont be needed
}

Additionally, you can console log in the zapier platform using ‘z.console.log’ which will help you debug all of this

Hi @Jeff Thorns 

I looked at your first function and believe that some of those variables may still be promises when Zapier tries to return them… 
 

async function actionHandeler(actionValue) {

var action = actionValue;
if (action == "cp_maken_&_relatie_maken") {


var maakRelatieWaarde = maakRelatie(AMRelatieData).then(response => { //i believe this variable is a promise try just a return statement instead
var POSTrelatieID = response;

AMContactData.Account = POSTrelatieID;

var maakContactwaarde = maakContact(AMContactData).then(response => { //this variable is a promise, try just a return statement instead
var POSTcontactID = response;

testcpid = POSTcontactID;
testrlid = POSTrelatieID;

return ('maakContactwaarde succes');
});

return (maakContactwaarde); //this is an unresolved promise. you can use return in the above and it wont be needed
});

return (maakRelatieWaarde); //this is an unresolved promise. you can use return in the above and it wont be needed
}

Additionally, you can console log in the zapier platform using ‘z.console.log’ which will help you debug all of this

hi, thank you so much I'm gonna try changing it, do you think i should add await like this?

 

var maakRelatieWaarde = await maakRelatie(AMRelatieData);

   

      var POSTrelatieID = maakRelatieWaarde;

Hi @Jeff Thorns 

I looked at your first function and believe that some of those variables may still be promises when Zapier tries to return them… 
 

async function actionHandeler(actionValue) {

var action = actionValue;
if (action == "cp_maken_&_relatie_maken") {


var maakRelatieWaarde = maakRelatie(AMRelatieData).then(response => { //i believe this variable is a promise try just a return statement instead
var POSTrelatieID = response;

AMContactData.Account = POSTrelatieID;

var maakContactwaarde = maakContact(AMContactData).then(response => { //this variable is a promise, try just a return statement instead
var POSTcontactID = response;

testcpid = POSTcontactID;
testrlid = POSTrelatieID;

return ('maakContactwaarde succes');
});

return (maakContactwaarde); //this is an unresolved promise. you can use return in the above and it wont be needed
});

return (maakRelatieWaarde); //this is an unresolved promise. you can use return in the above and it wont be needed
}

Additionally, you can console log in the zapier platform using ‘z.console.log’ which will help you debug all of this

hi, thank you so much I'm gonna try changing it, do you think i should add await like this?

 

var maakRelatieWaarde = await maakRelatie(AMRelatieData);

   

      var POSTrelatieID = maakRelatieWaarde;

i've followed your advice but although the code still functions i'm getting the same results

 

the updated code
 

var token = 'token';
var divisionid = 'divisionid';

var AMRelatieData = {
"Name": "company name",
"City": "city name",
"Website": "website.com"
};


var AMContactData = {
"FirstName": "firstname",
"LastName": "lastname",
"City": "name city"
};
var testrlid;

async function actionHandeler(actionValue) {

var action = actionValue;
if (action == "cp_maken_&_relatie_maken") {

var maakRelatieWaarde = await maakRelatie(AMRelatieData);

var POSTrelatieID = maakRelatieWaarde;

AMContactData.Account = POSTrelatieID;

var maakContactwaarde = await maakContact(AMContactData);
var POSTcontactID = maakContactwaarde;

testcpid = POSTcontactID;
testrlid = POSTrelatieID;

return ('maakContactwaarde succes');

}

//functions
async function updateRelatie(updateData, relatieId) {

var UpdateRelatiePUT = await PUTreq(1, updateData, relatieId);

console.log(UpdateRelatiePUT);

return ("updateRelatie succes");
}

async function maakRelatie(createData) {
var relatieId;

console.log('maakRelatie: ');

var maakRelatiePOST = await POSTreq(1, createData);

console.log('maakRelatieFunc:' + JSON.stringify(maakRelatiePOST));

return await maakRelatiePOST.d.ID;
}

async function maakContact(createData) {
var contactId;


var maaktcontactPOST = await POSTreq(2, createData);

console.log('maaktcontactFunc:' + JSON.stringify(maaktcontactPOST));


var jsonData = {
MainContact: maaktcontactPOST.d.ID
};

var relatieIdUpdate = createData.Account;
await updateRelatie(jsonData, relatieIdUpdate);
}

async function POSTreq(type, DATA) {
console.log('postreq');

var POSTendpoint = 'https://start.exactonline.nl/api/v1/'+ divisionid +'/crm/';
if (type == 1) {

POSTendpoint += 'Accounts';
}
if (type == 2) {

POSTendpoint += 'Contacts';
}

var outputPOST;
console.log(DATA);
await fetch(POSTendpoint, {
method: "POST",
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
},
body: JSON.stringify(DATA)
}).then(response => {
return response.json();
}).then(jsonResponse => {
var responseOut = jsonResponse;
outputPOST = responseOut;
}).catch(error => {
console.log(error);
});
return outputPOST;
}

async function PUTreq(type, DATA, id) {
var PUTendpoint = 'https://start.exactonline.nl/api/v1/'+ divisionid +'/crm/';

console.log('put data');
console.log(id);
console.log('data' + DATA);
console.log(type);

if (type == 1) {
PUTendpoint += "Accounts(guid'" + id + "')";
}
if (type == 2) {
PUTendpoint += "Contacts(guid'" + id + "')";
}

console.log(PUTendpoint);
console.log(PUTendpoint);
await fetch(PUTendpoint, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify(DATA)
});
}
}


async function actionHandlerFunc(){
console.log("begin");
await actionHandeler("cp_maken_&_relatie_maken");

return ("done did sum stuff");
};



output = [actionHandlerFunc()]

 

Userlevel 2
Badge

Hi @Jeff Thorns 

I looked at your first function and believe that some of those variables may still be promises when Zapier tries to return them… 
 

async function actionHandeler(actionValue) {

var action = actionValue;
if (action == "cp_maken_&_relatie_maken") {


var maakRelatieWaarde = maakRelatie(AMRelatieData).then(response => { //i believe this variable is a promise try just a return statement instead
var POSTrelatieID = response;

AMContactData.Account = POSTrelatieID;

var maakContactwaarde = maakContact(AMContactData).then(response => { //this variable is a promise, try just a return statement instead
var POSTcontactID = response;

testcpid = POSTcontactID;
testrlid = POSTrelatieID;

return ('maakContactwaarde succes');
});

return (maakContactwaarde); //this is an unresolved promise. you can use return in the above and it wont be needed
});

return (maakRelatieWaarde); //this is an unresolved promise. you can use return in the above and it wont be needed
}

Additionally, you can console log in the zapier platform using ‘z.console.log’ which will help you debug all of this

hi i followed your advise, but nothing is succesfull yet, i replied with another account here