0

I am trying to deploy a Firebase Function (a notification for the app) that fetches a value from a third party website. But, when I call the json, it says that that value does not exist in the error logs. Is there an obvious solution to this?

Follow up Edit: How do I get the data in the fetch method to be shown in the title?

 const url = new URL(
        "test.com"
    );
    let params = {
        "value": "title",
    };
    Object.keys(params)
        .forEach(key => url.searchParams.append(key, params[key]));

    fetch(url, {
        method: 'GET',
      })
      //This is where you usually would log the data .then(json => console.log(json));

      const payload = {
              notification: {
              title: json,
              body:  'The current events for today have been updated.',
              badge: '0',
              sound: 'default',
          }
      };
Chase Smith
  • 168
  • 7
  • `json` is not defined. – VLAZ Mar 16 '20 at 19:04
  • What is the correct way to get the json data from it then? – Chase Smith Mar 16 '20 at 19:05
  • I don't know. What is the intention here? Do you expect `json` to have some value? Do you want to just pass the string `"json"`? – VLAZ Mar 16 '20 at 19:07
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – VLAZ Mar 16 '20 at 19:26

2 Answers2

1

If I understand what you actually want to do, you are missing the .then() method of the fetch request. Something like:

fetch(url, {
    method: 'GET'    // You also had a wrong comma here
}).then(json => {
    // In here, json will contain the response from the fetch,
    // so you can use it however you prefer

    const payload = {
        notification: {
        title: json,
        body:  'The current events for today have been updated.',
        badge: '0',
        sound: 'default',
    }

    // do anything else

});
Aioros
  • 4,373
  • 1
  • 18
  • 21
  • This looks good, but it is giving me the error npm ERR! Failed at the functions@ lint script when I try and deploy it. – Chase Smith Mar 16 '20 at 19:29
  • Just edited, I assume it was the comma after `'GET'` from your snippet. – Aioros Mar 16 '20 at 19:35
  • Thanks! Now I'm getting a different error: error Parsing error: Unexpected token, and npm ERR! code ELIFECYCLE – Chase Smith Mar 16 '20 at 19:37
  • Well, I can only guess at those from experience: the unexpected token is probably from some part of your script trying to parse a JSON string into an object but actually not receiving valid JSON, and the ELIFECYCLE could be almost anything, maybe an unmanaged exception in an async method. Not something that we can see from your question, I'm afraid. – Aioros Mar 16 '20 at 20:34
0

You should update the payload variable. There is no variable called json as the value of title. Your payload should be

const payload = {
      notification: {
      title: 'json', // <-- mark here
      body:  'The current events for today have been updated.',
      badge: '0',
      sound: 'default',
   }
};

Update: I wrong understood the question. I think you want something like this-

    const url = new URL(
            "test.com"
        );
        let params = {
            "value": "title",
        };
        Object.keys(params)
            .forEach(key => url.searchParams.append(key, params[key]));

        const payload = {
                  notification: {
                  title: '',
                  body:  'The current events for today have been updated.',
                  badge: '0',
                  sound: 'default',
              }
          };

        fetch(url, {
            method: 'GET',
        }).then(res => res.json())
          .then(data => {
               payload.title = data.title; // Or something like that.
          });
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30