0

In my component, when I try to use substr on a string (in a object in a prop), I get the following error : Uncaught TypeError: offer.description.subtstr is not a function

The props are passed when the Axios request is complete in the parent component. I checked and it is a string value for description.

Here is my full code :

import React, { Component } from 'react'
import {Link} from 'react-router-dom'

export class ResultBox extends Component {
    render() {
        var offer = this.props.offer;
        var info = "| " + offer.address + " | " + offer.date;
        if(offer.minimumAge > 0) {
            info = info + " | Âge minimum : " + offer.minimumAge + " ans" 
        }
        return (
            <div key={offer.id} className="result-box">
                <div className="result-box-img"><img src={require('../../img/user/hands.svg')} /></div>
                <div>
                    <span className="result-box-org">{offer.organization.name}</span>
                    <span className="result-box-title"><Link to={"/organisme/offres/" + offer.id}>{offer.name}</Link></span>
                    <p className="result-box-desc">{offer.description.subtstr(0,10)}</p>
                    {(offer.placesAvailable > 0) ? 
                    <span className="result-box-nPlaces">{offer.placesAvailable} places disponibles {info}</span>
                    :
                    <span className="result-box-nPlaces">{offer.placesAvailable * -1} personnes dans la file d'attente  {info}</span>
                    }
                </div>
                <div className="result-box-date"></div>
            </div>
        )
    }
}

ResultBox.defaultProps = {
    offer : {description: ''}
}
export default ResultBox

```

3 Answers 3

3

it's substring()

https://www.w3schools.com/jsref/jsref_substring.asp

Try using

offer.description.substring(0, 10)

EDIT

you can use substr() as well..

but you have a typo in your code. What you have is subtstr()

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

2 Comments

I feel a little bit stupid rn, I should have seen the typo haha. Thank you so much, I don't know how much time I would have been stuck on this issue without your help...
@AsteroidSnowsuit no problem :) I have my shares in debugging my own typos too :p
0

looks like you have a typo. Should be substr not 'subtstr'

Comments

0

You need to destructure for that(substr) or use substring as solution already posted.

like

    let offer = {
      description:'Hey welcome to stack overflow'
    }
    const {description} = offer;
    
    document.write(description.substr(0,10));

Comments

Your Answer

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