0

data to be posted to the cloud function:

const body: object = {data: [{link: "https://links.someapp.app/JfzxSyzj1rAoMxQi8"}]}

the api call to invoke the cloud function:

const response: any = fetch("https://us-central1-someProject.cloudfunctions.net/getInfo", {method: 'POST', body: JSON.stringify(body), headers: {'Content-Type' : 'application/json'}}).then((data) => {console.log(data.url); return data.url});

The code of the function that when invoked throws the TypeError: Only absolute urls are supported:

export const shareIdFromShortLink = functions.https.onCall(async (data: any, context: CallableContext): Promise<string> => {
  let shareId: string = "";
  console.log("XXXXXX");
  try {
    console.log("YYYYYY");
    console.log("data", data)
    const res: any = await fetch(data.url);
    const url: string = res.url;
    const splitUrl = url.split('=');
    shareId = splitUrl[1];
  }catch(err:any){
    Sentry.captureException(err);
    throw new Error(
      `ERROR: Failed to get user shareId from dynamic shortlink at shareIdFromShortLink in pushNotifications.ts\nMESSAGE:${err} ${err.stack}`)
  }
  return shareId;
});

Oddly for some IoT there must have been a glitch when I initially deployed?After deleting the GCF and redeploying my function works and I have completed my function code:

 * Callable cloud function takes in a short link and returns the user's shareId
 * @param data
 * @param context
 * @returns {string} shareId
 */
export const shareIdFromShortLink = functions.https.onCall(async (data: object, context: CallableContext): Promise<string> => {
  let shareId: string = "";
  try {

    const link: object[] = Object.values(data).map((val: any) => {
      return Object.values(val).map((v: any) => v)
    });


    const url: string = link[0][0];
    const res: string = await fetch(url).then((x: any) => {
      return x.url;
    });


    const resArray: string[] = res.split('=');;
    shareId = resArray[1];

  } catch (err: any) {
    Sentry.captureException(err);
    throw new Error(
      `ERROR: Failed to get user shareId from dynamic shortlink at shareIdFromShortLink in pushNotifications.ts\nERROR MESSAGE:${err} ${err.stack}`)
  }

  return shareId;
});```
Shawna
  • 3
  • 4

0 Answers0