Skip to main content

Handling results

info

For more detail about the onResults callback, see the Reference page

Here we show how to handle results directly in the browser, using Google Firebase as an example (unlike an RDBMS like Postgres, Firebase allows for direct browser connections). To handle results on a server, either send the onResults data to an API you control or subscribe to a webhook.

This example assumes you have installed the Firebase SDK.

import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { addDoc, collection } from 'firebase/firestore';

const firebaseConfig = {
...
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

The onResults callback returns an array of objects, where each object represents a record. You can use a simple loop to write each record as a new document directly to a Firebase collection.


dromo.onResults(function (data) {
// Loop through each item in the data array and add it as a document in Firebase
data.forEach(async (record) => {
try {
const docRef = await addDoc(collection(db, 'collectionName'), record);
console.log(`Document written with ID: ${docRef.id}`);
} catch (e) {
console.error('Error adding document: ', e);
}
})
});