MethodsAdd Method | Fire Snippets Docs
Add Method
Adds a document to Firestore when a button is clicked.
add(snippetId, variables)
Description
Adds a new document to a specified Firestore collection when a button is clicked. It collects data from specified input fields and saves it to Firestore, replacing any placeholders with provided variables.
Method Signature
add(snippetId, variables)
Parameters
- snippetId: (string) The ID of the add snippet configuration.
- variables: (Array) An array of objects containing variable names and values to replace placeholders in the Firestore path.
Returns
- Promise: Resolves with a reference to the added document (
DocumentReference
), or rejects with an error.
Usage Example
// Initialize FireSnippets
const fireSnippets = new FireSnippets('your-api-key');
// Define variables for placeholders in the Firestore path
const variables = [
{ name: 'userId', value: '12345' }
];
// Call the add method
fireSnippets.add('your-add-snippet-id', variables)
.then(docRef => {
// Document added successfully
console.log('Document added with ID:', docRef.id);
})
.catch(error => {
// Handle errors
console.error('Error adding document:', error);
});
HTML Elements Required
Ensure your HTML includes the following elements with IDs matching those specified in your snippet configuration:
<!-- Input fields as per fieldMappings -->
<input type="text" id="name-input" placeholder="Name">
<input type="email" id="email-input" placeholder="Email">
<button id="add-button">Add Document</button>
Was this helpful?