所以我有一个注册功能,可以使用Firebase Auth对用户进行注册,然后将文档添加到Firestore。但是,当所有其他控制台日志正常工作时,Firestore甚至不返回错误。我从Firestore获得的唯一错误是有关新时间戳记的信息,以及如果没有时间戳记,则我的项目可能无法正常工作
const firestore = firebase.firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};
firestore.settings(settings);
注册功能完美运行
// Initialize Cloud Firestore through Firebase
const firestore = firebase.firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};
firestore.settings(settings);
function createEmail() {
var email = document.getElementById("emailInput").value;
var password = document.getElementById("passInput").value;
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.error(errorCode + ": " + errorMessage);
});
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
createDB((user), function() {
document.location.href = "home.html";
});
} else {
// User is signed out.
// ...
}
});
}
此功能中除
以外的所有内容firestore.collection...
工作正常。
function createDB(user, _callback) {
var fName = document.getElementById("fnameInput").value;
var lName = document.getElementById("lnameInput").value;
var owns = document.getElementById("option-own").checked;
var rents = document.getElementById("option-rent").checked;
var cottageNumber = document.getElementById("cottageNumber").value;
var cottageStreet = document.getElementById("cottageStreet").value;
var month = document.getElementById("birthMonth").value;
var day = document.getElementById("birthDay").value;
var year = document.getElementById("birthYear").value;
var dt = new Date();
var currYear = dt.getYear() + 1900;
var adult = false;
var youth = false;
if (currYear - year >= 20){
adult = true;
}else if (currYear - year <= 17) {
youth = true;
}else{
youth = true;
adult = true;
}
var userID = user.uid.toString();
console.log("User: " + user + " with ID: " + userID);
firestore.collection("users").doc(userID).set({
firstName: fName,
lastName: lName,
email: user.email,
doesOwn: owns,
isRenting: rents,
cottageNumber: cottageNumber,
cottageStreet: cottageStreet,
isDev: false,
isAdult: adult,
isYouth: youth
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
console.log("Collection Should Have Written!");
_callback();
}
编辑:我的Firestore规则是
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}