问题描述
我知道这可能是一个愚蠢的问题,但我想不通:(
<form @submit.prevent="customSubmit">
<label>Name</label>
<input type="text" v-model="newUser.name" id="name" placeholder="Your Name">
<label>E-mail:</label>
<input type="email" v-model="newUser.email" id="email" placeholder="Your email-address">
<label>Mobile Number</label>
<input type="number" v-model="newUser.number" id="number" placeholder="Your mobile number" @keyup.enter="customSubmit">
</form>
<button type="button" class=buttonSignup @click="customSubmit">Submit</button>
数据被处理并被提取到一个小nodeJS
-服务器,具有以下功能:
customSubmit(){
//Check ob alle Felder gefüllt sind
if(document.getElementById('name').value === ''){return}
if(document.getElementById('email').value === ''){return}
if(document.getElementById('number').value === ''){return}
//POST to API
const user = {
method: "POST",headers: { "Content-Type": "application/json"},body: JSON.stringify({newUser: this.newUser})
};
fetch("http://localhost:3080/api/user",user)
.then(response => response.json())
.then(data => console.log(data));
this.pushFunction(); //GET-Request to view the data
this.clearForm();
}
但是,为了也能够发送 DELETE
-Request,我需要为 customSubmit()
-function 中的每个条目分配唯一 ID。
谁能告诉我如何实现这一目标?
提前致谢!
PS:也请忽略代码片段中的德语注释。
解决方法
您应该让您的节点服务器创建一个条目并发送一个唯一 ID 作为对您发送的 POST 请求的响应。
ID 可以是数据库发布的 ID(取决于您使用的数据库),然后它将成为从 data
返回的 fetch
对象的一部分。