0

I send the object to the backend (Node js) when API call, but I check objects using console log. I checked using POSTMAN. When I checked POSTMAN, it worked very well, but I try to pass an object in the frontend (react js) it is shown as undefined.

  getnotification(){
  const userdetails={
  userid:"1235",
  username:this.state.name
  }
  axios.get('http://localhost:4000/notification/',userdetails)
  .then((res)=>{ this.setState((cur) => ({ ...cur, notification: res.data.reverse() }));

  })
}
1
  • in JavaScript you should name the variables and functions with camelCase (e.g. getNotification, userDetails). See more here Commented Apr 19, 2022 at 20:10

4 Answers 4

1

If you want to [POST], you need to use axios.post(url, body).

Following are the possible method signatures for axios.

axios.request(config)

axios.get(url[, config])

axios.delete(url[, config])

axios.head(url[, config])

axios.options(url[, config])

axios.post(url[, data[, config]])

axios.put(url[, data[, config]])

axios.patch(url[, data[, config]])

Read more https://axios-http.com/docs/api_intro/

Sign up to request clarification or add additional context in comments.

1 Comment

no.I want to use the get method. but I faced for that issue when some time post and patch methods.
1

You can use the async / await syntax instead of the callback function, but it's not supported by all browsers. And you can add error handling to the async function in simple ways.

async getNotification() {
        const userDetails = {
            userid: "1235",
            username: this.state.name
        }

        try {
            const res = axios.get('http://localhost:4000/notification/', userDetails)
            this.setState((cur) => ({...cur, notification: res.data.reverse()}));
        } catch (error) {
            console.log(error);
        }
    }

Comments

1

You are missing the "{}". You can do something like this:

const userDetails = {
      userid:'1235',
      username:this.state.name
    }
axios.get('/user', {
    params: userDetails
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {
    // always executed
  });  

Comments

0
async getNotification() {
        const userDetails = {
            userid: "1235",
            username: this.state.name
        }

        try {
            const res = await axios.get('http://localhost:4000/notification/', userDetails)
            this.setState((cur) => ({...cur, notification: res.data.reverse()}));
        } catch (error) {
            console.log(error);
        }
    }

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.