await is a reserved word error in ReactJS

10.1k views Asked by At

I'm new to ReactJS and I have used simple-oauth to connect to a test API. I have added the client id, client secret, username and password as well as the oauth token url. I'm getting syntax error await is a reserved word (40:21)

Below is my current code which is a sample from 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.

4

There are 4 answers

1
semanser On BEST ANSWER

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:

(async () => {
    const result = oauth2.ownerPassword.getToken(tokenConfig);
    const accessToken = oauth2.accessToken.create(result);
    // no console.log in the debugger
    console.log(result);
  })();
0
Roman On

You have to declare the componentWillMount (or componentDidMount) as async in order to use 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});
}
1
Mohit Mutha On

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.

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();
}
0
sg28 On
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,
  });
}

};