await 是 ReactJS 中的保留字错误
await is a reserved word error in ReactJS
我是 ReactJS 的新手,我已经使用 simple-oauth
连接到测试 API。我添加了客户端 ID、客户端密码、用户名和密码以及 oauth 令牌 url。我收到语法错误 await is a reserved word (40:21)
下面是我当前的代码,它是来自 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);
}
我也试过异步函数。虽然错误消失了,但控制台日志没有被触发。这是异步函数代码:-
async () => {
const result = oauth2.ownerPassword.getToken(tokenConfig);
const accessToken = oauth2.accessToken.create(result);
// no console.log in the debugger
console.log(result);
};
代码中可能有什么问题?请帮忙
代码行没有被触发。您将需要包装到异步函数中并从某个地方调用该函数 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();
}
I also tried async function. Though the error gone, the console log
isn't being triggered.
因为你没有调用你的函数。你需要的是 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);
})();
您必须将 componentWillMount
(或 componentDidMount
)声明为 async
才能使用 await
。您可以通过更改签名来做到这一点:
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,
});
}
};
我是 ReactJS 的新手,我已经使用 simple-oauth
连接到测试 API。我添加了客户端 ID、客户端密码、用户名和密码以及 oauth 令牌 url。我收到语法错误 await is a reserved word (40:21)
下面是我当前的代码,它是来自 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);
}
我也试过异步函数。虽然错误消失了,但控制台日志没有被触发。这是异步函数代码:-
async () => {
const result = oauth2.ownerPassword.getToken(tokenConfig);
const accessToken = oauth2.accessToken.create(result);
// no console.log in the debugger
console.log(result);
};
代码中可能有什么问题?请帮忙
代码行没有被触发。您将需要包装到异步函数中并从某个地方调用该函数 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();
}
I also tried async function. Though the error gone, the console log isn't being triggered.
因为你没有调用你的函数。你需要的是 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);
})();
您必须将 componentWillMount
(或 componentDidMount
)声明为 async
才能使用 await
。您可以通过更改签名来做到这一点:
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,
});
}
};