javascript - Firestore/Javascript:FirebaseError:使用无效数据调用的函数 DocumentReference.set()。不支持的字段值:未定义
问题描述
我不断收到此错误
创建用户时出错:FirebaseError:使用无效数据调用函数 DocumentReference.set()。不支持的字段值:未定义(在字段糖尿病并发症中找到)
我想我追踪到了获取所有复选框输入然后将它们存储到数组中的过程的问题。我也知道JSON.stringify()
在将它们存储在 Firestore 之前我必须使用这些代码。
无论如何,我需要帮助来准确查明问题的原因。因为在过去,我成功地完成了将数组存储到 firestore 的过程。
这是代码:
.JS
$('#registerForm').on('submit', async function (e) {
e.preventDefault();
var data = {
email: $('#email').val(), //get the email from Form
firstName: $('#fname').val(), // get firstName
lastName: $('#lname').val(), // get lastName
sex: $('#sex').val(),
birthDate: new Date($('#bday').val()),
diabetesType: parseInt($('#dtype').val()),
weight: parseInt($('#weight').val()),
height: parseInt($('#height').val()),
};
var allergyList = [];
var cou = i.counter;
for (c = 0; c <= cou; c++) {
var allergyField = document.getElementById("allergy" + c).value;
allergyList.push(allergyField);
AllergyString = JSON.stringify(allergyList)
};
var arrayComplications = [];
var checkboxes = document.querySelectorAll('input[type=checkbox]:checked');
for (var i = 0; i < checkboxes.length; i++) {
JSON.stringify(arrayComplications.push(checkboxes[i].value))
}
var passwords = {
password : $('#password').val(), //get the pass from Form
cPassword : $('#cpassword').val(), //get the confirmPass from Form
}
if( data.email != '' && passwords.password != '' && passwords.cPassword != '' ){
if( passwords.password == passwords.cPassword ){
//create the user
firebase.auth().createUserWithEmailAndPassword(data.email, passwords.password).then(function(user){setTimeout(function(){
console.log('uid',user.user.uid);
usersRef.doc(user.user.uid).set({
'email': data.email, 'firstName': data.firstName,
'lastName': data.lastName, 'allergies': allergyList,
'sex': data.sex, 'birthDate': firebase.firestore.Timestamp.fromDate(data.birthDate),
'diabetesType': data.diabetesType, 'diabetesComplication': arrayComplications,
'weight': data.weight, 'height': data.height, 'firstLogin': true,
})}, 3000)
.then(function(){
console.log("User Information Saved:", user.user.uid);
window.alert("User Created!");
window.location.href = "patientDashboard.php";
})
})
.catch(function(error){
console.log(arrayComplications);
console.log("Error creating user: ", error);
console.log("Error creating user: ", user.user.uid);
window.alert("Error creating user:" + error);
});
}
}
});
这是代码:
HTML
<div class="col-md-6 pl-1">
<div class="form-group">
<label>Diabetes Complication</label>
<br>
<input type="checkbox" name="type" value="No Complications" /><b>No Complications</b>
<br>
<input type="checkbox" name="type" value="Retinopathy" /><b>Retinopathy</b>
<br>
<input type="checkbox" name="type" value="Neuropathy" /><b>Neuropathy</b>
<br>
<input type="checkbox" name="type" value="Nephropathy" /><b>Nephropathy</b>
<br>
<input type="checkbox" name="type" value="Cardiovascular"/><b>Cardiovascular</b>
</div>
</div>
</div>
注意代码
var allergyList = [];
var cou = i.counter;
for (c = 0; c <= cou; c++) {
var allergyField = document.getElementById("allergy" + c).value;
allergyList.push(allergyField);
AllergyString = JSON.stringify(allergyList)
};
就是我之前所说的成功完成整个存储过程。
编辑
这是我遇到问题的代码:
var arrayComplications = [];
var checkboxes = document.querySelectorAll('input[type=checkbox]:checked');
for (var i = 0; i < checkboxes.length; i++) {
JSON.stringify(arrayComplications.push(checkboxes[i].value))
}
我似乎无法获取复选框值、存储到数组中、将其序列化并将其存储到 firestore 中。
解决方案
错误消息准确地告诉您出了什么问题。您正在调用 set() 并向其传递一个对象,该对象包含undefined
名为diabetesComplication
. 这就是这里发生的事情 - 我重新格式化了代码以使其更易于阅读:
usersRef.doc(user.user.uid).set({
'email': data.email,
'firstName': data.firstName,
'lastName': data.lastName,
'allergies': allergyList,
'sex': data.sex,
'birthDate': firebase.firestore.Timestamp.fromDate(data.birthDate),
'diabetesType': data.diabetesType,
'diabetesComplication': arrayComplications, // this contains undefined
'weight': data.weight,
'height': data.height,
'firstLogin': true,
})
diabetesComplicatation
分配给的行arrayComplicatation
- 某处有一个未定义的值。Firestore 根本不接受未定义的值,因为没有等效的表示形式。您必须调试代码以找出未定义值的来源,或者确保在传递给set()
.
也许您可以在推入之前检查for
循环中未定义的值。(而且我不确定您为什么要尝试对推送的结果进行字符串化。这似乎在任何地方都没有效果。)这取决于您进行调试,因为我们在这里看不到所有值。checkboxes[i].value
arrayComplications
推荐阅读
- c# - 如何在 csproj 中检查条件编译符号
- sql - 在 Postgresql 中使用列值上的 where 子句更新表
- django - 如何在 django rest 框架中传递多个 authentication_classes?
- regex - 正则表达式超过 n 个字符,不包括计数中的模式
- event-handling - 一个平台的 UI 框架是否为你提供了主循环的行话?
- javascript - 如何通过 keydown 事件在插入符号上插入元素?
- python - 如何使用 PyTorch 修剪/删除张量的一部分以匹配另一个张量的形状?
- jenkins - 如何编写一个 jenkins 文件一起定义 5 个存储库
- c - Richard F. Gilberg 的数据结构书中的 createlist 函数声明在 c 中的正确转换是什么
- c - 使用 sscanf 从字符串中获取输入