如何通过module.exports从axios响应中读取数据? [英] How to read data from axios response through module.exports?

查看:51
本文介绍了如何通过module.exports从axios响应中读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过module.export从axios响应中读取数据.这是我的数据: http://localhost:8001

I need to read data from axios response through module.export. This is my data: http://localhost:8001

{
    "name":"Nama Nih",
    "desc":"Sekolah Terpadu Al-Qudwah - Yayasan Islam Qudwatul Ummah",
    "prefix":"alqudwah",
    "footerText":"Yayasan Islam Qudwatul Ummah | All Rights Reserved 2020",
    "logoText":"Al-Qudwah",
    "needLogin":false
}

我使用以下代码从axios调用数据: brand.js

I call data from axios with this code: brand.js

var axios = require('axios');

module.exports = axios.get('http://localhost:8001', {
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json'
  }})
  .then(function (response) {
    return response.data;
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {
  });

但是当我读取响应数据时,它与预期的不一样.我尝试这样控制台日志返回值:

But when I read response data it's not like expected. I try to console log return value like this:

这是我在控制台输入代码的地方.记录响应.

This is my code where I console.log the response.

import React, { PureComponent } from 'react';
import brand from 'ba-api/brand';
import { Helmet } from 'react-helmet';

class Dashboard extends PureComponent {
  render() {
    console.log(brand);
    const title = brand.name + ' - Dashboard';
    const description = brand.desc;
    const { classes } = this.props;
    return (
      <div>
        <Helmet>
          <title>{title}</title>
          <meta name="description" content={description} />
          <meta property="og:title" content={title} />
          <meta property="og:description" content={description} />
          <meta property="twitter:title" content={title} />
          <meta property="twitter:description" content={description} />
        </Helmet>
      </div>
    );
  }
}

Dashboard.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(Dashboard);

如何读取此数据?

推荐答案

我已经通过以下解决方案解决了这个问题: brand.js

I have solved this question with this solution: brand.js

var axios = require('axios');

const sendGetRequest = async () => {
  try {
      const resp = await axios.get('http://localhost:8001', {
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Content-Type': 'application/json'
        }})
        .then(function (response) {
          return response.data;
        });
      return resp;
} catch (err) {
      console.error(err);
  }
};

module.exports = sendGetRequest();

然后我用以下代码调用该函数:

And I call that function with this code:

import React, { PureComponent } from 'react';
import brand from 'ba-api/brand';
import { Helmet } from 'react-helmet';

class Dashboard extends PureComponent {
  constructor(props){
    super(props);
    this.state = {
      title: '',
      description: ''
    }  
  }
  componentDidMount(){
    brand.then(data => {
      this.setState({
        title: data.name,
        description: data.desc
      });  
    });
  }
  render() {
    const title = this.state.title + ' - Dashboard';
    const description = this.state.description;
    const { classes } = this.props;
    return (
      <div>
        <Helmet>
          <title>{title}</title>
          <meta name="description" content={description} />
          <meta property="og:title" content={title} />
          <meta property="og:description" content={description} />
          <meta property="twitter:title" content={title} />
          <meta property="twitter:description" content={description} />
        </Helmet>
      </div>
    );
  }
}

Dashboard.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(Dashboard);

感谢所有回答我问题的人.

Thank you for all who responses my question.

这篇关于如何通过module.exports从axios响应中读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆