Мне нужно экспортировать результат ответа axios на module.exports. Это мои коды:
brand.js
var axios = require('axios');
module.exports = (async function() {
try {
const { data } = axios.get('http://localhost:8000/api/v1/setting/index');
console.log(data.data.initial);
return {
name: data.data.name,
desc: data.data.description,
};
} catch (err) {
console.log(err);
}
})();
Я пытаюсь импортировать результат для использования в другом файле. Это мой код.
import React from 'react';
const {brand} = await require("brand.js");
class Dashboard extends Component {
render(){
const name = brand.name
const desc = brand.description;
return (
<h1>{title} | {description}</h1>
);
}
}
Результат моего кода:
Can not use keyword 'await' outside an async function
Это ошибка, отображаемая в браузере:
Как это решить?
await
передaxios.get()
в brand.js вместоrequire()
? - person BlackMath   schedule 19.11.2020.then()
для импортированного (обязательного) объекта Promise. - person Randy Casburn   schedule 19.11.2020