JavaScript выборки API ведут себя по-разному локально и на сервере - PullRequest
0 голосов
/ 04 июля 2018

Это мой код,

 fetch('/auth', {
      credentials: 'include',
      mode: 'cors'
    }) // or whatever URL you want
      .then((response) => response.json())
      .then((data) => {
        console.log(data.token + ' ' + data.username);
        this.setState({
          token: data.token,
          username: data.username
        })
        return fetch("/api/openshift/projects?token=" + this.state.token + "&username=" + this.state.username,
          {
            credentials: 'include',
            mode: 'cors'
          })
      })
      .then((response) => response.json())
      .then(
      (result) => {
        this.setState({
          isLoaded: true,
          fetchInProgress: false,
          data: result.map(project => createData(project.project_name, project.project_description, project.creation_time, 'Launch'))
        });

        return fetch("/api/openshift/project/getimage?token=" + this.state.token,
          {
            credentials: 'include',
            mode: 'cors'
          })
      },
      (error) => {
        this.setState({
          isLoaded: true,
          error
        });
      }
      )
      .then(res => res.json())
      .then(
      (result) => {
        this.setState({
          isLoaded: true,
          fetchInProgress: false,
          images: JSON.parse(JSON.stringify(result))
        });
        console.log('images' + result);
      },
      (error) => {
        this.setState({
          isLoaded: true,
          error
        });
      }
    )
  }

Проблема в том, что я называю API nested fetch. Первые два раза выборка работает нормально, но в третий раз, /api/openshift/project/getimage?token= когда я вызываю этот API, я получаю пустое result.

Этот же код отлично работает на localhost, но когда я отправляю код на сервер (в нашем случае openshift), третий API не работает.

Почему третья выборка не работает в третьем случае?

Сведения об ошибке:

    {this.props.imagesarray.filter(image => (image.image_name =='algo'))[0].tags.map(option => (
       <MenuItem key={option.name} value={option.name}>
        {option.name}

и ошибка, которую я получаю в браузере:

TypeError: Cannot read property 'tags' of undefined
...