1

I will explain my problem to you today: the following code works fine, I get my database, then I use a map function, so everything is fine. My question and the next, I get a table in it I have an object that contains an array. This table has the following form:

 [{"title":"Lagunitas IPA (Demi-Pinte","quantity":2}]

and it is possible to recover one of the two pieces of data because when I map over the array I get all of it.

Do you have an idea of how to fix this?

    import React, { Component } from 'react';
    import { CardText,  Col } from 'reactstrap';
    import axios from 'axios'

    const entrypoint = process.env.REACT_APP_API_ENTRYPOINT+'/api';

    class AdminPage  extends Component {

    constructor(props) {
        super(props);
        this.state = {
            data: [],   
              },
        };
    }

    getRandom = async () => {

        const res = await axios.get(
            entrypoint + "/alluserpls"
        )
        this.setState({ data: res.data })
    }
    componentDidMount() {
        this.getRandom()
    }
    render() {

        let datas = this.state.data.map(datass => {
          return (
              <div >
                  <Col sm="12" key={datass.id}">
                          <CardText> Commande{datass.items}</CardText>
                  </Col>
              </div>
          )
      })
        return (
        <div> 
        {datas}
      </div>
        )
     }
   }

export default AdminPage 
5
  • try debuging and seeing what exactly is in datass.items and datass.id. you may need to access datass.items[0] and datass.items[1] however this is only an educated guess Commented Feb 7, 2020 at 13:45
  • {datass.title} will show title of each Commented Feb 7, 2020 at 13:48
  • inside <CardText> Commande{datass.title}</CardText> like this I mean Commented Feb 7, 2020 at 13:49
  • @bwright { "id": 118, "title": "brand", "items": "[{\"title\":\"Lagunitas IPA (Demi-Pinte\",\"quantity\":2}]" } Commented Feb 7, 2020 at 13:53
  • @PramodNikam i cant <CardText> Commande{datass.title}</CardText> because on my table i have " title" Commented Feb 7, 2020 at 13:53

2 Answers 2

1

when you want to get a data from server , it is in json format so it can transfer from server to client. so when you receive on the client side , make sure to parse it, this will give you the data back into an object form that u can use each property of your data in mapping them.

lets assume you are receiving this data

 [{"title":"Lagunitas IPA (Demi-Pinte","quantity":2}]

this data in json format ,now you need to parse it to be able to use the properties seperately

in your getRandom method

getRandom = async () => {

   const res = await axios.get(
      entrypoint + "/alluserpls"
   )
   let parsedData = JSON.parse(res.data);
   this.setState({ data: parsedData  })
}

now you have the data stored as an array of objects, in render you can map it and use it as

let datas = this.state.data.map((datass,index) => {
    return (
       <div>
           <Col sm="12" key={index}">
               <Col sm="6">
               Title : {datass.title}
               </Col>
               <Col sm="6">
               Quantity: {datass.quantity}
               </Col>
           </Col>
       </div>
    )
 })

this will show the value of quantity and title separately.

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

8 Comments

i got error AdminPage.js:83 Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>)
you need to consider if the data you are getting in the res is json or not first.. the data u posted above is accurate.
Hi , the problem i think and that i have both format in the same table. What do you think ?my table looks like this to be precise { "id": 118, "name": "", "phone": "1643416502", "randomID": "456", "title": "", "quantity": "", "items": "[{\"title\":\"Lagunitas IPA (Demi-Pinte\",\"quantity\":2}]" }
and I really want to recover Items in two parts
{ "id": 118, "name": "", "phone": "1643416502", "randomID": "456", "title": "", "quantity": "", "items": "[{\"title\":\"Lagunitas IPA (Demi-Pinte\",\"quantity\":2}]" } is also in json , you should be able to parse it, u can check in the console if it is the exact data you receive in res.data
|
0
    getRandom = async () => {
            const res = await axios.get(
                entrypoint + "/alluserpls"
            )
            const data = res.json();
            this.setState({ data })
        }

7 Comments

and for inside<CardText> Commande{}</CardText> what is the code if i want to take only "quantity " for " items" ?
let datas = this.state.data.map(datass => { return ( <Col sm="12" key={datass.id}"> <CardText> Commande{datass.quantity}</CardText> </Col> ) })
i have error , Uncaught (in promise) TypeError: t.json is not a function at AdminPage.js:82 at l (runtime.js:45) at Generator._invoke (runtime.js:264) at Generator.next (runtime.js:98) at r (asyncToGenerator.js:3) at u (asyncToGenerator.js:25)
quantity string or number?
this schema "items": "[{\"title\":\"Lagunitas IPA (Demi-Pinte\",\"quantity\":2}]" } so is string i guess
|

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.