我的問題是通過從表單中獲取信息將數據添加到數據庫。 我想將信息添加爲「名稱」。我可以正確添加「電子郵件」,但不能添加其他數據。將數據添加到數據庫中的問題
我的代碼:
buttonsignup.addEventListener('click', error => {
var nameCompany = document.getElementById('nameCompany').value;
var email = document.getElementById('email').value;
});
function add(nameCompany,email) {
firebase.database().ref().child('users_company').push({
nameCompany: nameCompany,
email: email
});
}
function intFirebase() {
/*CURRENT USER*/
firebase.auth().onAuthStateChanged(function(user) {
if (user != null) {
console.log(user);
console.log('El UID es '+user.uid);
add(nameCompany,user.email);
} else {
console.log('No user is signed in.');
}
});
}
window.onload = function() {
intFirebase();
}
好,把咖啡倒進代碼後。我發現這個解決方案。但是...這是一個好習慣嗎?
const database = firebase.database();
var user = firebase.auth().currentUser;
/* BOTON SIGNUP */
buttonsignup.addEventListener('click', error => {
\t var nameCompany = document.getElementById('nameCompany').value;
\t var email = document.getElementById('email').value;
\t var password_1 = document.getElementById('password_1').value;
\t var password_2 = document.getElementById('password_2').value;
\t
\t if (password_1 == password_2) {
\t \t if (password_1.length < 8) {
\t \t \t console.log('Contraseña muy corta');
\t \t \t document.getElementById("errorPassword").innerHTML = "8 characters";
\t \t } else {
\t \t \t firebase.auth().createUserWithEmailAndPassword(email, password_1).then (function(result) {
\t \t \t \t add(nameCompany,email);
\t \t \t }).catch(function (error) {
\t \t \t \t var errorCode = error.code;
\t \t \t \t var errorMessage = error.message;
\t \t \t \t };
\t \t \t });
\t \t }
\t } else{
\t \t console.log('errorPassword');
\t }
});
function add(nameCompany,email) {
\t firebase.database().ref().child('users_company').push({
\t \t nameCompany: nameCompany,
\t \t emailCompany: email
\t });
}
function intFirebase() {
\t /*CURRENT USER*/
\t firebase.auth().onAuthStateChanged(function(user) {
\t \t if (user != null) {
\t \t \t console.log(user);
\t \t \t console.log('El UID es '+user.uid);
\t \t } else {
\t \t \t console.log('No user is signed in.');
\t \t }
\t });
}
window.onload = function() {
\t intFirebase();
}
和數據庫規則
{
"rules": {
"users_company": {
"$uid": {
".read": "auth != null && auth.uid === $uid",
".write": true,
//Change this code to: I can not do scripts in the database. -> ".write": "auth != null && auth.uid === $uid",
"nameCompany" : {
\t ".validate": "newData.isString() && newData.val().length < 100"
},
"emailCompany" :{
\t ".validate": "newData.isString() && newData.val().length < 100"
}
}
}
}
}
你需要在問題標題中更具體(專業)。不是「數據庫」,而是「Firebase數據庫」。雖然您在標籤列表中擁有Firebase,但這還不夠好。祝你好運。 – Reid