M
M
mrrangerr2020-07-17 22:14:46
Node.js
mrrangerr, 2020-07-17 22:14:46

Why is the error Cannot set headers after they are sent to the client coming out?

I'm getting an error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
What's the problem?

// код на сервере

app.get('/profile', (req, res) => {
    if (req.session.userId) {
        User.findOne({
            raw: true,
            where: {
                id: req.session.userId
            }
        })
            .then(user => {
                if (!user) {
                    return res.status(404).send({ message: "Что-то пошло не так" })
                }
                res.send({
                    name: user.username,
                    email: user.email
                })
            })
    }
        res.send('something')
})

//код на клиенте
export default class Profile extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            profileData: {}
        }
        this.getDataProfile = this.getDataProfile.bind(this)
    };

    componentDidMount(){
        this.getDataProfile()
    }

    async getDataProfile(){
        const request = await fetch('http://localhost:5000/profile', {
            headers: {
                'Content-Type': 'application/json',
            }
        })
        const response = await request.json()
        this.setState({
            profileData: response
        })
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question