0

i want to create a post end send to api but i need to retrive userId to create my post.

( frontend : vuejs3 + axios backend : node + prisma + sql)

Test on postman is ok but i cannot retrive usedId in vue js. can you help me please ?

import axios from "axios"
export default {
    name: 'PostCreate',
    data () {
        return {
            title: '',
            content: '',
        }
    },
    methods: {
        async createPost(req) {
            const token = Authorization.split(' ')[1];

            // dechiffre le token a l'aide de la clé secrete et du token presant dans authorization
            const decodedToken = jwt.verify(token, `${process.env.MY_TOKEN}`);
            console.log('decodedToken');
            const response = await axios.post('http://localhost:3000/api/post', {

                headers: {
                    Authorization: 'Bearer ' + localStorage.getItem('token')
                },
                
                title: this.title,
                content: this.content,
                userId: decodedToken.userId
            });
        }
    }
}

1 Answer 1

1

You should define headers as the 3rd argument.

const response = await axios.post('http://localhost:3000/api/post', {                
                title: this.title,
                content: this.content,
                userId: decodedToken.userId
            }, {
 headers: { Authorization: 'Bearer ' + localStorage.getItem('token') },
});
Sign up to request clarification or add additional context in comments.

1 Comment

and how i can acces to my decodedToken.userid ?

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.