Fetch not executing in routers.js

I am new to all of this, so all of your help is appreciated. I started with the sample router.js code.

Task #1 I want to use data received from my external API call. When it is done outside of routers.js (in the Site code), it works like a charm. But when it is in routers.js, I can’t even get any of the console log to output anything…

Here is the start of my code…

import {
	ok, notFound, WixRouterSitemapEntry
}
from "wix-router";
 import {fetch} from 'wix-fetch';

export function agent_Router(request) {
	
    let myID = "testuser";
    let url = '/api/getUserProfile/' + myID;

	fetch(url, {
		method: 'get'
	})
	.then((httpResponse) => {
		if (httpResponse.ok) {
			console.log("here 1");
			return httpResponse.json();

		} else {
			console.log("here 2");
			return Promise.reject("Fetch did not succeed");
		}
	})
	.then((json) => {
		console.log("here 3");
		console.log(JSON.stringify(json));

	})
	.catch(err => {
		console.log("here 4");
		console.log(err);

	});
.
.
.

There are no errors in the developer console.

Once I get this working, I need to replace this sample block with the results from my API call.

const peopleData will need be be changed to the result of my API call. First things first.

That will be another thread I’m sure… :slight_smile:

Thx

Hi,

You are creating a fetch request to ‘/api/getUserProfile/testuser’.
The url is missing the host part. For example ‘www.userdatabases.com/api/getUserProfile/testuser

In addition, note that router functions are automatically called by a backend script. In your function you are returning the json response to the function caller, which is in your case the backend script, essentially losing the data.
Use the following call to set the router response data

Currently when working with backend scripts, 'console.log’s commands will only return a value to the console if you call them from the global scope of the backend script.
'console.log’s executed inside a promise are ignored.