Home > database >  In nested functions only the first function works
In nested functions only the first function works

Time:06-21

I've never used the nested functions before, and this seemingly simple task is giving me trouble. When I run this code only the first function work, and the second is completely unresponsive.

const getCMSID = () => {
    let url = `${URL}/store-api/category`;
    let getId = {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        'sw-access-key': `${API_KEY}`,
      },
    };
    fetch(url, getId)
      .then(res => res.json())
      .then(json => console.log(json.elements[0].cmsPageId))
      .catch(err => console.error('error:'   err));

    const getCMSPage = () => {
      const cmsUrl = `${URL}/store-api/${id}`;
      let getcms = {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          Accept: 'application/json',
          'sw-access-key': `${API_KEY}`,
        },
      };
      fetch(cmsUrl, getcms)
        .then(res => res.json())
        .then(json => {
          console.log(json.sections);
          setLoading(false);
        })
        .catch(err => console.error('Error:'   err));
    };
  };

CodePudding user response:

It doesnt look like you ever call getCMSPage

const getCMSID = () => {
    let url = `${URL}/store-api/category`;
    let getId = {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        'sw-access-key': `${API_KEY}`,
      },
    };
    fetch(url, getId)
      .then(res => res.json())
      .then(json => console.log(json.elements[0].cmsPageId))
      .catch(err => console.error('error:'   err));

    const getCMSPage = () => {
      const cmsUrl = `${URL}/store-api/${id}`;
      let getcms = {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          Accept: 'application/json',
          'sw-access-key': `${API_KEY}`,
        },
      };
      fetch(cmsUrl, getcms)
        .then(res => res.json())
        .then(json => {
          console.log(json.sections);
          setLoading(false);
        })
        .catch(err => console.error('Error:'   err));
    };
    getCMSPage()
  };
  • Related