I want to add a document to a cloud firestore collection when clicking a button
I can add data in using the following when loading a page and not having it as a function
But for some reason it won't work when trying to use onclick on a button to call it as a function.
function addStudent(){
const db = firebase.firestore();
// Add a new document in collection "students"
const student = db.collection("students").doc("BobJones");
student.set({
first_name: "Bob",
last_name: "Jones",
email: "jobesb@email.co.uk",
completion_year: 2019,
student_number: 1020
})
.then(function() {
console.log("Document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
};
<button type="submit" class="btn btn-primary" onclick="addStudent()">
Submit
</button>
When running this without using a button and just on page load it works fine.
When using a button it doesn't work, I don't even get an error message so I am not sure if the onclick is triggering properly?
Thanks in advance :)