如何为输入表单中的输入分配唯一 ID

问题描述

我知道这可能是一个愚蠢的问题,但我想不通:(

我有一个输入表单:

<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 对象的一部分。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...