Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在提交到firebase firestore之前,如何使用javascript更改所有空值?_Javascript_Firebase_Forms_Google Cloud Firestore - Fatal编程技术网

在提交到firebase firestore之前,如何使用javascript更改所有空值?

在提交到firebase firestore之前,如何使用javascript更改所有空值?,javascript,firebase,forms,google-cloud-firestore,Javascript,Firebase,Forms,Google Cloud Firestore,我试图允许不需要的未答复表单输入仍然能够提交到Firebase Firestore。问题是它返回的null不是对象。如果您没有使用firebase,它不允许您提交null值。我正在试图找出如何将所有null值输入更改为可提交的值 下面是我正在尝试做的一个例子: if (document.querySelector('input').value.length == 0) { this.value = 'No input' } 当前html: <input type="

我试图允许不需要的未答复表单输入仍然能够提交到Firebase Firestore。问题是它返回的
null不是对象。如果您没有使用firebase,它不允许您提交
null
值。我正在试图找出如何将所有
null
值输入更改为可提交的值

下面是我正在尝试做的一个例子:

if (document.querySelector('input').value.length == 0) {
    this.value = 'No input'
  }
当前html:

<input type="text" id="fname" name="fname" required>
<input type="text" id="lname" name="lname" required>
<input type="email" id="email" name="email" required>

<p>Radio 1</p>

<div>
  <label for="a1">Option 1</label>
  <input type="radio" id="a1" name="a" value="Value 1">
</div>

<div>
  <label for="a2">Option 2</label>
  <input type="radio" id="a2" name="a" value="Value 2">
</div>

<div>
  <label for="a3">Option 3</label>
  <input type="radio" id="a3" name="a" value="Value 3">
</div>

<div>
  <label for="b">Question 1</label>
  <textarea id="b" name="b" rows="5" placeholder="Your Response…"></textarea>
</div>

<div>
  <label for="c">Question 2</label>
  <input id="c" name="c" placeholder="Your Response…">
</div>

在代码中,在if语句之后使用
this
,这就是错误所在。这是因为
指的是窗口,因为它不是事件。下面是你的代码应该看起来有点像li




此外,更好的方法是在分配变量时更改变量,如下所示:

  var a = document.querySelector('[name="a"]:checked').value || "No val";
  var b = document.querySelector('[name="b"]').value || "No val";
  var c = document.querySelector('[name="c"]').value || "No val";

你能分享完整表格的代码吗?我们可能会找到最好的方法,你可以通过数百种方法来做到这一点。例如,您可以获取表单中的所有输入并对其进行迭代,或者只使用
必需的
属性(请参见我的答案)。您可能希望查看行“db.collection”(“Team Forms”).doc('Feedback Survey')。set',因为错误
null不是一个对象
表明问题出在您的一个对象中,而不是字符串。但由于我没有使用firebase,我不能确定,正如您所说,问题可能出在字符串中。如果是这样的话,看看我的答案。对不起。我编辑了我的问题,使它更容易理解。我需要提交未答复的表单输入。也就是说,输入的.length==0。好的,知道了。我已经编辑了我的答案,希望它能有所帮助。也许我实现错误了,但它仍然返回相同的错误。我不确定您的代码的哪一部分只获得空/空输入来更改它们。请确保在回答中的javascript之前执行上述代码。或者,现在您已经添加了正在使用的js代码,您可以使用以下内容:
“问题1”:b | |“no val”,
对于objectBtw中的每个值,我的代码中只获取空/空输入来更改它们的部分是if语句
!输入.value
。执行
input.value='no val'输入的
时,才使用code>。
let all_inputs = document.querySelectorAll("input"); // If you have multiple forms, use ("#form1 input");
all_inputs.forEach(input => {
   if(!input.value) {
      input.value = "No value";
   }
});
  var a = document.querySelector('[name="a"]:checked').value || "No val";
  var b = document.querySelector('[name="b"]').value || "No val";
  var c = document.querySelector('[name="c"]').value || "No val";