z3r0
z3r0

Reputation: 139

Vuejs- Get Object ID after POST Request In Current Page

I want to implement Save and Edit at same page. Of course, i have alot of field input so, i can Input a few input field and save Without Rediect to another page.

What i want is get current id after POST Request so, i can use That ID to PATCH request.

Vuejs Code

<v-btn
    color="primary"
    v-if="isEdit === false"
    small
    :loading="loading"
    @click="save"
    >save</v-btn
>
<v-btn
    color="primary"
    small
    :loading="loading"
    @click="edit"
    v-if="isEdit === true"
    >edit</v-btn
>

In script

<script>
export default {
   data() {
        return {
          form: {},
          isEdit: false
       }
   },

     save() {
            this.loading = true;
            axios
                .post(`api/v1/partner/`, this.form)
                .then((res) => {
                    console.log(res);
                    this.isEdit = true;
                })
                .catch((err) => {
                    console.log(err.response);
                    this.loading = false;
                    this.snackbar.value = true;
                    this.$refs.form.validate(err.response.data);
                });
        },

        edit() {
            this.isEdit = true;
            axios
                .patch(`api/v1/partner/${this.form.id}/`, {
                })
                .then((res) => {
                    console.log(res);
                    // this.$router.push(`/partner/`);
                    this.loading = false;
                })
                .catch((err) => {
                    console.log(err.response);
                    this.loading = false;
                });
        },
}
</script>

I'll appreciate of all ur Help. Thanks...

Upvotes: 0

Views: 800

Answers (1)

Phil
Phil

Reputation: 164729

Assuming your API responds from the POST request with the new ID, you can simply set it to your form object

axios.post("api/v1/partner/", this.form)
  .then(res => {
    console.log(res)
    this.isEdit = true
    this.form.id = res.data.id // assuming that's the right property name
  })

Upvotes: 1

Related Questions