Мне нужно передать зарегистрированный идентификатор пользователя в серверную часть, и у меня есть хранилище vuex, поэтому я могу получить свою информацию о пользователе, например {{currentUser.id}}
, проблема в том, что я не могу передать ее в серверную часть, это дает мне ошибку проверки, что требуется user_id
, пока у меня это скрыто ввод в мою форму
<input type = "hidden" name = "user_id" :value = "currentUser.id">
для обычных входов у меня есть v-model
, например v-model = "project.title"
, который невозможно использовать в скрытых полях.
The question here is how can I pass my
user_id
to back-end?
<script>
import validate from 'validate.js';
export default {
data: function () {
return {
project: {
title: '',
body: '',
attachment: '',
projectclass: '',
deadline: '',
user_id: '',
csrf: document.querySelector('meta[name = "csrf-token"]').getAttribute('content'),
},
errors: null
}
},
computed: {
currentUser() {
return this.$store.getters.currentUser;
}
},
methods: {
add() {
this.errors = null;
const errors = validate(this.$data.project);
if (errors) {
this.errors = errors;
return;
}
axios.post('/api/projects/new', this.$data.project)
.then((response) => {
this.$router.push('/projects');
});
}
}
}
</script>
Это происходит потому, что user_id в this. $ Data.project не обновляется.
Вместо скрытого ввода вы можете просто сделать
add() {
this.errors = null;
const errors = validate(Object.assign(this.$data.project, {user_id: this.currentUser.id}));
if (errors) {
this.errors = errors;
return;
}
axios.post('/api/projects/new', Object.assign(this.$data.project, {user_id: this.currentUser.id}))
.then((response) => {
this.$router.push('/projects');
});
}