0

I have mock data to which I want to add values. In particular there is a users object and each user has a rating property which is an array of numbers from 1 to 5. I would like to add further values to this array. I tried it like this:

this.http.post(`api/users?id=${userId}/rating`, rate, this.cudOptions)

but no value is added to the array.

This is the users object:

export var users = [
    {
        "id": 0,
        "firstName": "Stanford",
        "lastName": "Reilly",
        "username": "StanfRei",
        "password": "StanfRei",
        "email": "[email protected]",
        "cars": [...],
        "recentCars": [...],
        "observedCars": [...],
        "rating": [
            3,
            5,
            3,
            2,
            1,
            3,
            5,
            5,
            5,
            2,
            5,
            1
        ]
    },
    {...}
]

I have no problem recovering data with get.
I have no errors.
I have not found anything that helps neither in the official documentation nor on the internet.
How do you post to insert a new value in the rating array of the user? Thanks

1 Answer 1

0

There is no code for other dependencies like InMemDataService. So its hard to assume your source. May be you can use put instead of post to update.

this.http.put(`api/users?id=${userId}/rating`, rate, this.cudOptions)

If it doesn't works try following, You have to use the whole user object as request body, instead of rate alone.

With same user Id, send complete user object with updated rate array, it should works.

let user = {
    "id": 0,
    "firstName": "Stanford",
    "lastName": "Reilly",
    "username": "StanfRei",
    "password": "StanfRei",
    "email": "[email protected]",
    "cars": [...],
    "recentCars": [...],
    "observedCars": [...],
    "rating": [
        3,
        5
    ]
}

this.http.put(`api/users/${user.id}`, user)
Sign up to request clarification or add additional context in comments.

Comments

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.