动态字段在通过端点保存到数据库时存在问题。

huangapple go评论64阅读模式
英文:

Dynamic fields having issue saving to DB through endpoint

问题

我遇到了在尝试通过端点保存此内容时的问题,当我尝试在后端的每个模型字段上指定null来完成此操作时,代码的其余部分都正常工作,但代码的这个特定部分的动态字段未保存到数据库中,出现了未捕获的ReferenceError: data未定义。

以下是您提供的代码的翻译部分:

import React, { useState } from 'react'

const [inputField, setInputFields] = useState([
    { vin: '', year: '', make: '', model: '', vtype: '', qty: '', color: '', plate: '', lot: '', vstate: '', info: '' }
])

const handleChangeInput = (index, event) => {
    const values = [...inputField]
    values[index][event.target.name] = event.target.value;
    setInputFields(values)
    console.log(index, values)
}

const handleAddField = () => {
    const newFields = { vin: '', year: '', make: '', model: '', vtype: '', qty: '', color: '', plate: '', lot: '', vstate: '', info: '' }
    setInputFields([...inputField, newFields])
}

const handleRemoveField = (index) => {
    const values = [...inputField];
    values.splice(index, 1);
    setInputFields(values);
}

const handleSubmit = (e) => {
    e.preventDefault()
    const postData = {
        vin: inputField.vin, year: inputField.year, make: inputField.make,
        model: inputField.model, vtype: inputField.vtype, qty: inputField.qty,
        color: inputField.color, plate: inputField.plate, lot: inputField.lot,
        vstate: inputField.vstate, info: inputField.info,
    }
    fetch('http://127.0.0.1:8000/api/post/', {
        method: 'POST',
        headers: {
            "Content-type": "application/json"
        },
        body: JSON.stringify(postData)
    }).then(response => response.json())
        .then(data => console.log(data))
        .catch(error => console.log(error))
}

// JSX部分
{inputField.map((inputField, index) => (
    <div key={index} className="card-body">
        <div className="col-sm-12">
            <div className="form-group">
                <label htmlFor="inputVin"></label>
                <input
                    type="text"
                    id="inputVin"
                    value={inputField.vin}
                    placeholder="Vin Number"
                    className="form-control"
                    name="vin"
                    onChange={event => handleChangeInput(index, event)}
                />
            </div>
        </div>
        {/* 其余部分的翻译类似,但是包括其他字段 */}
        {/* ... */}
        <div className="form-group col-2 float-right">
            {index > 0 ?
                <button
                    type="button"
                    onClick={() => handleRemoveField(index)}
                    className="form-control btn btn-danger"
                >
                    Remove
                </button>
                :
                ""
            }
        </div>
    </div>
))}

请注意,我只翻译了代码的部分,不包括注释和描述。如果您需要更多帮助或有其他问题,请随时提问。

英文:

i have issue saving this through the endpoint when i tried to get this done specifying null on each models fields in the backend the rest of the code works fine, but this particular area of the code dynamic fields isn't save to database, Uncaught ReferenceError: data is not defined

import React, {useState} from &#39;react&#39;
const [inputField, setInputFields] = useState([
{vin:&#39;&#39;, year:&#39;&#39;, make:&#39;&#39;, model:&#39;&#39;, vtype:&#39;&#39;, qty:&#39;&#39;, color:&#39;&#39;, plate:&#39;&#39;, lot:&#39;&#39;, 
vstate:&#39;&#39;, info:&#39;&#39;}
])
const handleChangeInput= (index, event) =&gt; {
const values = [...inputField]
values[index][event.target.name] = event.target.value;
setInputFields(values)
console.log(index, values)
}
const handleAddField = () =&gt; {
const newFields ={vin:&#39;&#39;, year:&#39;&#39;, make:&#39;&#39;, model:&#39;&#39;, vtype:&#39;&#39;, qty:&#39;&#39;, color:&#39;&#39;, 
plate:&#39;&#39;, lot:&#39;&#39;, vstate:&#39;&#39;, info:&#39;&#39;}
setInputFields([...inputField, newFields])
}
const handleRemoveField = (index) =&gt; {
const values = [...inputField];
values.splice(index, 1);
setInputFields(values);
}
const handleSubmit = (e) =&gt; {
e.preventDefault()
const postData = {
vin: inputField.vin, year: inputField.year, make: inputField.make, 
model: inputField.model, vtype: inputField.vtype, qty: inputField.qty, 
color: inputField.color, plate: inputField.plate, lot: inputField.lot, 
vstate: inputField.vstate, info: inputField.info, 
}
fetch(&#39;http://127.0.0.1:8000/api/post/&#39;, {
method: &#39;POST&#39;,
headers: {
&quot;Content-type&quot;: &quot;application/json&quot;
},
body: JSON.stringify(postData)
}).then(response =&gt; response.json())
.then(data = console.log(data))
.catch(error =&gt; console.log(error))
}

this is the part for the jsx

    {inputField.map((inputField, index) =&gt; (
&lt;div key={index} className=&quot;card-body&quot;&gt;
&lt;div className=&quot;col-sm-12&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputVin&quot;&gt;&lt;/label&gt;
&lt;input 
type=&quot;text&quot; 
id=&quot;inputVin&quot; 
value={inputField.vin}
placeholder=&quot;Vin Number&quot; 
className=&quot;form-control&quot; 
name=&quot;vin&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;row&quot;&gt;
&lt;div className=&quot;col-sm-2&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputYear&quot;&gt;&lt;/label&gt;
&lt;input 
type=&quot;text&quot; 
id=&quot;inputYear&quot; 
placeholder=&quot;* Year&quot; 
value={inputField.year}
className=&quot;form-control&quot; 
name=&quot;year&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;col-sm-3&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputMake&quot;&gt;&lt;/label&gt;
&lt;input 
type=&quot;text&quot; 
id=&quot;inputMake&quot; 
placeholder=&quot;* Make&quot; 
value={inputField.make}
className=&quot;form-control&quot; 
name=&quot;make&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;col-sm-3&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputModel&quot;&gt;&lt;/label&gt;
&lt;input 
type=&quot;text&quot; 
id=&quot;inputModel&quot; 
placeholder=&quot;* Model&quot; 
className=&quot;form-control&quot; 
value={inputField.model}
name=&quot;model&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;col-sm-3&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputType&quot;&gt;&lt;/label&gt;
&lt;select 
id=&quot;inputType&quot; 
value={inputField.vtype}
className=&quot;form-control custom-select&quot;
name=&quot;vtype&quot;
onChange={event =&gt; handleChangeInput(index, event)}
&gt;
&lt;option selected disabled&gt;Select Vehicle Type&lt;/option&gt;
&lt;option value=&quot;ATV&quot;&gt;ATV&lt;/option&gt;
&lt;option value=&quot;Boat&quot;&gt;Boat&lt;/option&gt;
&lt;option value=&quot;Car&quot;&gt;Car&lt;/option&gt;
&lt;option value=&quot;Heavy-Equipment&quot;&gt;Heavy Equipment&lt;/option&gt;
&lt;option value=&quot;Large-Yatch&quot;&gt;Large Yatch&lt;/option&gt;
&lt;option value=&quot;MotorCycle&quot;&gt;MotorCycle&lt;/option&gt;
&lt;option value=&quot;Pickup&quot;&gt;Pickup&lt;/option&gt;
&lt;option value=&quot;RV&quot;&gt;RV&lt;/option&gt;
&lt;option value=&quot;SUV&quot;&gt;SUV&lt;/option&gt;
&lt;option value=&quot;Trailer&quot;&gt;Trailer&lt;/option&gt;
&lt;option value=&quot;Van&quot;&gt;Van&lt;/option&gt;
&lt;option value=&quot;Other&quot;&gt;Other&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;col-sm-1&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputQty&quot;&gt;&lt;/label&gt;
&lt;input 
type=&quot;text&quot; 
id=&quot;inputQty&quot; 
placeholder=&quot;Qty&quot; 
value={inputField.qty}
className=&quot;form-control&quot; 
name=&quot;qty&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;row&quot;&gt;
&lt;div className=&quot;col-sm-3&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputColor&quot;&gt;&lt;/label&gt;
&lt;input 
type=&quot;text&quot; 
id=&quot;inputColor&quot; 
placeholder=&quot;Vehicle Color&quot; 
value={inputField.color}
className=&quot;form-control&quot; 
name=&quot;color&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;col-sm-3&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputPlate&quot;&gt;&lt;/label&gt;
&lt;input 
type=&quot;text&quot; 
id=&quot;inputPlate&quot; 
placeholder=&quot;Plate Licence&quot; 
value={inputField.plate}
className=&quot;form-control&quot; 
name=&quot;plate&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;col-sm-3&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputLot&quot;&gt;&lt;/label&gt;
&lt;input 
type=&quot;text&quot; 
id=&quot;inputLot&quot; 
placeholder=&quot;Lot Number&quot; 
value={inputField.lot}
className=&quot;form-control&quot; 
name=&quot;lot&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className=&quot;col-sm-3&quot;&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputState&quot;&gt;&lt;/label&gt;
&lt;select 
id=&quot;inputState&quot; 
className=&quot;form-control custom-select&quot;
value={inputField.vstate}
name=&quot;vstate&quot;
onChange={event =&gt; handleChangeInput(index, event)}
&gt;
&lt;option selected disabled&gt;State &amp; Province&lt;/option&gt;
&lt;option value=&quot;Alabama&quot;&gt;Alabama&lt;/option&gt;
&lt;option value=&quot;Alaska&quot;&gt;Alaska&lt;/option&gt;
&lt;option value=&quot;Arizona&quot;&gt;Arizona&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;/div&gt;                      
&lt;/div&gt;
&lt;div className=&quot;form-group&quot;&gt;
&lt;label htmlFor=&quot;inputInfo&quot;&gt;Additional Vehicle Information&lt;/label&gt;
&lt;textarea 
id=&quot;inputInfo&quot; 
className=&quot;form-control&quot; 
rows={4}
value={inputField.info} 
name=&quot;info&quot;
onChange={event =&gt; handleChangeInput(index, event)}
/&gt;
&lt;/div&gt;
&lt;div className=&quot;form-group col-2 float-right&quot;&gt;
{ index &gt; 0 ?
&lt;button 
type=&quot;button&quot; 
onClick={() =&gt; handleRemoveField(index)}
className=&quot;form-control btn btn-danger&quot;
&gt;
Remove
&lt;/button&gt;
:
&quot;&quot;
}
&lt;/div&gt;
&lt;/div&gt;
))}

答案1

得分: 1

尝试这样做。我认为这是一个语法错误。

>
.then(data = console.log(data)) ---> .then(data => console.log(data))

英文:

Try this. I think this is a syntax error

>
.then(data = console.log(data)) ---> .then(data => console.log(data))

huangapple
  • 本文由 发表于 2023年7月14日 02:52:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定