gpt4 book ai didi

javascript - 使用表单将数据发布到 Firebase

转载 作者:行者123 更新时间:2023-12-03 00:48:58 25 4
gpt4 key购买 nike

我有一个带有输入的简单表单:

<body>
<div class="jumbotron">
<div class="placeholder"></div>
</div>
<div class="container">
<h2>Add a new Animal</h2>
<form class="form-group" id="add-animals">
Species: <input type="text" id="species" class="form-control">
Name: <input type="text" id="name" class="form-control" >
Age: <input type="text" id="age" class="form-control">
Last Fed: <input type="date" id="last-fed" class="form-control">
Last Shed: <input type="date" id="last-shed" class="form-control">
Diet: <input type="text" id="diet" class="form-control">
Basking area temperature: <input type="text" id="basking-area-temperature" class="form-control">
Cold part temperature: <input type="text" id="cold-temperature" class="form-control">
Humidity: <input type="text" id="humidity" class="form-control">
Addition Info: <textarea class="form-control" id="additional-info"></textarea>
<button id="btnCreate" class="btn btn-primary">Create</button>
</form>
<h3>View Current Animals</h3>
<ol id="animal-list">

</ol>
</div>
</div>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyBSuC8nqJzLe7d5jKS-_nE15kaI9Y6NIfI",
authDomain: "repti-care-32176.firebaseapp.com",
databaseURL: "https://repti-care-32176.firebaseio.com",
projectId: "repti-care-32176",
storageBucket: "repti-care-32176.appspot.com",
messagingSenderId: "632910932105"
};
firebase.initializeApp(config);
const db = firebase.firestore();
db.settings({ timestampsInSnapshots: true });
</script>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>

<script src="./scripts/main.js"></script>
</body>

我想在提交表单时向数据库添加新条目,这是我的 js:

const animalList = document.querySelector('#animal-list');
const form = document.querySelector('#add-animals');


function renderAnimals(doc) {
let li = document.createElement('li');
let name = document.createElement('div');
let species = document.createElement('div');
let age = document.createElement('div');
let last_fed = document.createElement('div');
let last_shed = document.createElement('div');
let diet = document.createElement('div');
let basking_area_temp = document.createElement('div');
let cold_part_temp = document.createElement('div');
let humidity = document.createElement('div');
let additional_info = document.createElement('div');

li.setAttribute('data-id', doc.id);
name.textContent = `name: ${doc.data().name}`
species.textContent = `species: ${doc.data().species}`
age.textContent = `age: ${doc.data().age}`
last_fed.textContent = `last fed: ${doc.data().last_fed}`;
last_shed.textContent = `last shed: ${doc.data().last_shed}`;
diet.textContent = `diet: ${doc.data().diet}`;
basking_area_temp.textContent =`basking area temp: ${ doc.data().basking_area_temp}`;
cold_part_temp.textContent = `cold part temp: ${doc.data().cold_part_temp}`;
humidity.textContent = `humidity: ${doc.data().humidity}`;
additional_info.textContent = `additional info: ${doc.data().additional_info}`;

li.appendChild(species);
li.append(name);
li.append(age);
li.append(last_fed);
li.append(last_shed);
li.append(diet);
li.append(basking_area_temp);
li.append(cold_part_temp);
li.append(humidity);
li.append(additional_info);

animalList.appendChild(li);
}

// getting data from the back end
db.collection('animals').get().then((snapshot) => {
snapshot.docs.forEach(doc => {
renderAnimals(doc);
})
})

// adding data
form.addEventListener('submit', (event) => {
event.preventDefault();
db.collection('animals').add({
species: form.species.value,
name: form.name.value,
age: form.age.value,
last_fed: form.last_fed.value,
last_shed: form.last_shed.value,
diet: form.diet.value,
basking_area_temp: form.basking_area_temp.value,
cold_part_temp: form.cold_part_temp.value,
humidity: form.humidity.value,
additional_info: form.additional_info.value

})
})

目前,当我将信息添加到表单字段时,它会引发错误:

无法读取未定义的属性“值” 在 HTMLFormElement.form.addEventListener (main.js:58)

为什么我的last_fed对象属性未定义,如何解决这个问题以便将数据发布到数据库?由于某些原因,只有其中带有“_”符号的变量才会出现问题,如果我删除它们,一切都会正常

最佳答案

您正在初始化表单,如下所示:

const form = document.querySelector('#add-animals');

然后在提交处理程序中执行以下操作:

species: form.species.value

form.species 没有指向任何东西。 DOM 中没有任何内容允许您通过 ID 来查找表单中的字段。

您需要使用查询选择器从 HTML 中查找每个字段。所以对于这一行:

species: document.querySelector("species").value

请注意,一旦您在引发错误的代码中放置断点并用断点击中它,这种类型的问题就很容易解决。从那里您可以检查 form 变量并发现它没有 species 属性。

关于javascript - 使用表单将数据发布到 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com