Using fetch - not working :(

Hi guys,

First of all, thanks for this amazing product, what you’ve done lately opening wix so you can add back end functionality is really good.

I’m trying to use the wix-fetch api but I’m not getting any response back once the fetch is sent. I’m debugging it using console.log, the last console.log triggered is this: console.log(headers);
I’ve obfuscated the API keys for obvious reasons. Please find snippet below:

import {fetch} from ‘wix-fetch’;
const CLIENT_ID = “blablabla”;
const CLIENT_SECRET = “blebleble”;
export function addToEmailList (address) {
console.log(“back end function raised!”);
var data={
grant_type:‘client_credentials’,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET
};
console.log(data);
console.log(JSON.stringify(data));
var headers = { ‘Content-Type’:‘application/json’, ‘Content-Length’: JSON.stringify(data).length.toString() };

console.log(headers); 
fetch("https://api.sendpulse.com/oauth/access_token", { 
         method: 'POST',              
         headers: headers, 
         body: JSON.stringify(data) }) 
 .then(response => { 
 	console.log("1"); 
	return response.json() 
		.then(data => { 
	      if (response.ok) { 
	      	console.log("2"); 
	        return data; 
	      } else { 
	      	console.log("3"); 
	        return Promise.reject({status: response.status, data}); 
	      } 
}); 

})
.then(result => console.log(‘success:’, result))
.catch(error => console.log(‘error:’, error));
}

You would probably need a return before the fetch to see the console logs even if the fetch was working as explained here: Velo: Testing and Troubleshooting Your Code | Help Center | Wix.com.

Also, have you tried opening the browser’s console (not the Wix Code console) to see if there are any errors logged there?

Hi Sam,

All good now :slight_smile: It was as you said, the return missing.

Thanks a lot for your help!