[英]await is a reserved word error in ReactJS
I'm new to ReactJS and I have used simple-oauth
to connect to a test API.我是 ReactJS 的新手,我使用simple-oauth
连接到测试 API。 I have added the client id, client secret, username and password as well as the oauth token url.我已经添加了客户端 ID、客户端密码、用户名和密码以及 oauth 令牌 url。 I'm getting syntax error await is a reserved word (40:21)
我收到语法错误await is a reserved word (40:21)
Below is my current code which is a sample from simple-oauth:-以下是我当前的代码,它是来自 simple-oauth 的示例:-
const credentials = {
client: {
id: "CLIENT_ID",
secret: "CLIENT_SECRET"
},
auth: {
tokenHost: "http://localhost/oauth/token"
}
};
const oauth2 = require('simple-oauth2').create(credentials);
const tokenConfig = {
username: "USERNAME",
password: "PASSWORD",
scope: '<scope>',
};
try {
const result = await oauth2.ownerPassword.getToken(tokenConfig);
const accessToken = oauth2.accessToken.create(result);
} catch (error) {
console.log('Access Token Error', error.message);
}
I also tried async function.我也试过异步功能。 Though the error gone, the console log isn't being triggered.虽然错误消失了,但没有触发控制台日志。 Here's the async function code:-这是异步函数代码:-
async () => {
const result = oauth2.ownerPassword.getToken(tokenConfig);
const accessToken = oauth2.accessToken.create(result);
// no console.log in the debugger
console.log(result);
};
What could be wrong in the code?代码中可能有什么问题? please help.请帮忙。
I also tried async function.我也试过异步功能。 Though the error gone, the console log isn't being triggered.虽然错误消失了,但没有触发控制台日志。
Because you didn't call your function.因为你没有调用你的函数。 What you need is a Self Invoking Function
:你需要的是一个Self Invoking Function
:
(async () => {
const result = oauth2.ownerPassword.getToken(tokenConfig);
const accessToken = oauth2.accessToken.create(result);
// no console.log in the debugger
console.log(result);
})();
The lines of code are not getting triggered.代码行没有被触发。 You will need to wrap into the async function and call the function from somewhere componentDidMount
will be a good place.您将需要包装到 async 函数中并从某个地方调用该函数componentDidMount
将是一个好地方。
const funcName = async () => {
const result = await oauth2.ownerPassword.getToken(tokenConfig);
const accessToken = oauth2.accessToken.create(result);
// no console.log in the debugger
console.log(result);
};
componentDidMount(){
this.funcName();
}
You have to declare the componentWillMount
(or componentDidMount
) as async
in order to use await
.您必须将componentWillMount
(或componentDidMount
)声明为async
才能使用await
。 You can do that by changing the signature:您可以通过更改签名来做到这一点:
async componentWillMount() {
const result = await oauth2.ownerPassword.getToken(tokenConfig);
const resultJson = await result.json();
const accessToken = await oauth2.accessToken.create(resultJson);
const accessTokenJson = await accessToken.json();
//if you need
this.setState({
accessTokenJson,
});
//For debugging
console.log('result', {resultJson, accessTokenJson});
}
handleSubmit = async (e) => {
e.preventDefault();
console.log("form Submit ", this.state);
const { email, password } = this.state;
try {
const config = {
"content-type": "application/json",
};
this.setState({ loading: true });
const { data } = await axios.post(
"/api/users/login",
{
email,
password,
},
config
);
} catch (e) {
console.error("Error Login!", e);
this.setState({
error: e.response.data.message,
});
}
}; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.