User Creation Api Is Returning Status 204 In Content

Asked

Viewed 89 times

2

I’m developing an api that I get the id the user of the request through the token, I store in my user object and I register this user. The problem is that it is not returning anything, even if I put user. If I put one console.log(user) before the return my object is as expected (user information that was registered + token, but the api returns Status 204 + No content.

I tried to:

const User = use("App/Models/User")

class UserController {

    store ({ request, auth }){

        let user = request.all()

        this.usuarioLogado(auth).then(async res => { 
            user.user_id = res.id
            user = await User.create(user)
            const token = await auth.generate(user)
            Object.assign(user, token)
            return user
        })
    }

    async usuarioLogado(auth) {
        try {
            return await auth.getUser()
        } catch (error) {
            console.log(error)
        }
    }
}
  • Shouldn’t be return this.usuarioLogado(auth).then...?

  • no, I tried that way. I had to assign the user’s return to a constant, and then return that constant.

1 answer

0

I don’t have the Adonis on my machine to simulate this test. But reading, it seems to be only something related to Javascript. I don’t know what its variable token is returning. But, let’s assume that it has a value returned as:

const token = { value: 'asdo12398j129e12v9ne9182u2098343847230m4m03' };

And let’s also assume that user has the following output:

const user = {
    name: 'Thiago'
};

That is, returning an object... So it makes sense to have the function Object.assign...

The exit would be...

{
  name: 'Thiago',
  value: 'asdo12398j129e12v9ne9182u2098343847230m4m03'
}

That’s the same as user.value = token.value.

Try to explore the following exit: {user, token}... the output would make more sense, because token is not son of user (believe), and yes an object that only has a relationship in common with 'user'.

See also the exit of Token to know if it is being printed as well. This is important for me to update this question... because I don’t know the behavior of Toke value as well as its structure coming from the query.

Use the object Response from Adonis to return the result. It already contains a nice structure to return what you need, as well as work on the Output Status.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.