javascript - 无法在 firebase 上上传数据?

标签 javascript html firebase firebase-realtime-database

我正在尝试在 firebase 上上传一个简单的表单数据,但问题是我什至没有收到任何错误或任何上传的数据或确认警报框。

有什么建议我哪里出错了吗?

我正在使用 firebase 实时数据库并遵循它的文档

这是我的 Firebase 实时数据库的链接。

https://iet-workshop.firebaseio.com/

var name = document.getElementById("name");
var email = document.getElementById("email");
var msg = document.getElementById("comment");

function submitClick(){
	

	var nameTxt = name.value;
	var emailTxt = email.value;
	var msgTxt = msg.value;

	writeUserData(nameTxt,emailTxt,msgTxt);

}

function writeUserData(name, email, msg) {
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    comment : msg
  }, function(error){
  	if(error){
  		// write failed
  		alert("Sorry unable to write data error = ",error);
  	}else{
  		// data saved successfully
  		alert("Thanks for your feedback");
  	}
  });
}
body{
	background: royalblue;
}

.form-style-6{
	font: 95% Arial, Helvetica, sans-serif;
	max-width: 400px;
	margin: 10px auto;
	padding: 16px;
	background: #F7F7F7;
}
.form-style-6 h1{
	background: #43D1AF;
	padding: 20px 0;
	font-size: 140%;
	font-weight: 300;
	text-align: center;
	color: #fff;
	margin: -16px -16px 16px -16px;
}
.form-style-6 input[type="text"],
.form-style-6 input[type="date"],
.form-style-6 input[type="datetime"],
.form-style-6 input[type="email"],
.form-style-6 input[type="number"],
.form-style-6 input[type="search"],
.form-style-6 input[type="time"],
.form-style-6 input[type="url"],
.form-style-6 textarea,
.form-style-6 select 
{
	-webkit-transition: all 0.30s ease-in-out;
	-moz-transition: all 0.30s ease-in-out;
	-ms-transition: all 0.30s ease-in-out;
	-o-transition: all 0.30s ease-in-out;
	outline: none;
	box-sizing: border-box;
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	width: 100%;
	background: #fff;
	margin-bottom: 4%;
	border: 1px solid #ccc;
	padding: 3%;
	color: #555;
	font: 95% Arial, Helvetica, sans-serif;
}
.form-style-6 input[type="text"]:focus,
.form-style-6 input[type="date"]:focus,
.form-style-6 input[type="datetime"]:focus,
.form-style-6 input[type="email"]:focus,
.form-style-6 input[type="number"]:focus,
.form-style-6 input[type="search"]:focus,
.form-style-6 input[type="time"]:focus,
.form-style-6 input[type="url"]:focus,
.form-style-6 textarea:focus,
.form-style-6 select:focus
{
	box-shadow: 0 0 5px #43D1AF;
	padding: 3%;
	border: 1px solid #43D1AF;
}

.form-style-6 input[type="submit"],
.form-style-6 input[type="button"]{
	box-sizing: border-box;
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	width: 100%;
	padding: 3%;
	background: #43D1AF;
	border-bottom: 2px solid #30C29E;
	border-top-style: none;
	border-right-style: none;
	border-left-style: none;	
	color: #fff;
}
.form-style-6 input[type="submit"]:hover,
.form-style-6 input[type="button"]:hover{
	background: #2EBC99;
}
<!DOCTYPE html>
<html>
<head>
	<title>IET Workshop</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<div class="form-style-6">
		<h1>Contact Us</h1>
		<form>
			<input id="name" type="text" name="field1" placeholder="Your Name" />
			<input id="email" type="email" name="field2" placeholder="Email Address" />
			<textarea id="comment" name="field3" placeholder="Type your Message"></textarea>
			<input id='submitBtn' type="submit" value="Send" onclick="submitClick()" />
		</form>
	</div>
</body>


<script src="https://www.gstatic.com/firebasejs/5.7.3/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "AIzaSyD83SJTFRLbWE2oh8ov7IPBBNQaSX_kpBs",
    authDomain: "iet-workshop.firebaseapp.com",
    databaseURL: "https://iet-workshop.firebaseio.com",
    projectId: "iet-workshop",
    storageBucket: "iet-workshop.appspot.com",
    messagingSenderId: "569222277610"
  };
  firebase.initializeApp(config);
</script>
<script type="text/javascript" src="main.js"></script>
</html>

最佳答案

这听起来像是规则不允许访问数据库的典型情况。检查规则并确保它允许写入

"rules": 
{
         ".read": true,
         ".write": true,
}

关于javascript - 无法在 firebase 上上传数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54223827/

相关文章:

html - 如何让 HTML5 canvas 文本显示 html 实体?

swift - Firebase 独特值(value)

javascript - HTML5、JavaScript : Drag and Drop File from External Window (Windows Explorer)

javascript - 列表状态更新时 react native 平面列表图像闪烁

特定字段的 firebase 读/写规则

javascript - 如何更改fullcalendar中ajax发送的 'start'和 'end'参数?

javascript - JavaScript 中文本输入行为中的 onchange 事件在键入时立即注册更改?

javascript - 下一个 Js 错误 : Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined

javascript - 如何在多边形上打洞以在 Google map 上以干净的颜色显示每个多边形?

javascript - 使用 .getElementById 抓取元素时为 Null