I'm trying to code a simple form that gets a single user input and the rest are const variables that sets(creates) a new sub-document in an existing document. Currently I am being thrown an error:
Uncaught (in promise) TypeError: firebase.firestore(...).collection(...).doc(...).collection(...).set is not a function
Here is code:
<form id="bgForm" method="POST">
<div class="wrap-input100 validate-input m-t-25 m-b-35" data-validate="Enter blood glucose">
<h5>Update Blood Glucose</h5>
<font face="Montserrat">Input Blood Glucose: </font>
<input class="input100" type="text" name="inputBloodGlucose" style="font-family:Montserrat;" id="inputBg" required />
<button type="submit" class="btn btn-primary" id="updateBG">Update Blood Glucose</button>
</form>
<script>
(function(){
//Update BG
$('#bgForm').on('submit', async function (e) {
e.preventDefault();
var data = {
bgReading: parseInt($('#inputBg').val()), //get bg int
dateAdded: firebase.firestore.Timestamp.fromDate(new Date()),
isActive: true,
};
firebase.auth().onAuthStateChanged(function(user){
if(user){
this.userId = user.uid; //stores the userid
}
firebase.firestore().collection("users").doc(userId).collection("glucose").set({
'bgReading': data.bgReading, 'dateAdded': data.dateAdded,
'isActive': data.isActive,
})
.then(function(){
window.alert("Blood Glucose updated!");
// window.location.href = "diabetesManagement.php";
})
.catch(function(error){
console.log("Error updating blood glucose", error);
window.alert("Error updating blood glucose" + error);
});
});
});
})();
</script>
</div>
I am using
firebase.auth().onAuthStateChanged(function(user){
if(user){this.userId = user.uid;}
To get uid
of the currently logged in user and then store it in variable. Can I not use this code to perform queries? Because that is the only thing I'm currently leaning on as the reason why an error is being thrown at me.