Why is my test_query in Firebase Admin SDK throwing an error?

This topic was automatically generated from Slack. You can find the original thread here.

Hi why is my test_query throwing error

import firebase_admin_sdk from@pipedream/firebase_admin_sdk”;

export default defineComponent({
props: {
async run({ steps, $ }) {
const original_query = {
select: { fields: },
from: [
collectionId: “pipedream_log_darwinboxSwiftEmployeeDataFetch”,
allDescendants: false,

_// only get documents that were created from today_
**const** now = **new** Date();
**const** currentDayStart = **new** Date(
  Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())

**const** test_query = {
  select: { fields: [] },
  **from**: [
      collectionId: "collection-id",
      allDescendants: **true**,
  where: {
    fieldFilter: {
      field: { fieldPath: "createTime" },
      op: "GREATER_THAN",
      value: {
        timestampValue: currentDayStart.toISOString(),

**try** {
  **const** original_results = **await** **this**.firebase_admin_sdk.runQuery(original_query);
  $.**export**("original_results", original_results);
  **const** test_results = **await** **this**.firebase_admin_sdk.runQuery(test_query);
  $.**export**("test_results", test_results);
} **catch** (e) {
  $.**export**("error", e)


The code snippet you’ve provided is attempting to use the Firebase Admin SDK to run queries against a Firestore database. However, there are a few potential issues with the code that might be causing the error you’re encountering: 1. Incorrect Import Statement: The import statement for the Firebase Admin SDK is not correct for use within Pipedream components. Pipedream components use a specific format for integrating with external services, which involves defining an app prop in the component’s props object. This is not done through a regular import statement like in a standalone Node.js application. 2. Missing Firebase Admin SDK Initialization: Before making any queries to Firestore using the Firebase Admin SDK, you need to initialize the SDK with your Firebase project’s credentials. This involves setting up the SDK with a service account key and initializing it accordingly. 3. Incorrect Method for Running Queries: The method this.firebase_admin_sdk.runQuery() does not exist in the Firebase Admin SDK. To run queries against Firestore, you need to use the Firestore API methods provided by the Firebase Admin SDK after initializing it. Here’s a revised version of your code that addresses these issues: ``` import admin from “firebase-admin”; export default defineComponent({ props: { firebase_admin_sdk: { type: “app”, app: “firebase_admin_sdk”, }, }, async run({ steps, $ }) { const { projectId, clientEmail, privateKey } = this.firebase_admin_sdk.$auth; const formattedPrivateKey = privateKey.replace(/\n/g, “\n”); if (!admin.apps.length) { admin.initializeApp({ credential: admin.credential.cert({ projectId, clientEmail, privateKey: formattedPrivateKey, }), }); } const db = admin.firestore(); const collectionRef = db.collection(“collection-id”); const now = new Date(); const currentDayStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())); const testQuery = collectionRef.where(“createTime”, “>”, currentDayStart); try { const originalResults = ; // You need to replace this with your actual query for original_results $.export(“original_results”, originalResults); const testResultsSnapshot = await testQuery.get(); const testResults = testResultsSnapshot.docs.map(doc => doc.data()); $.export(“test_results”, testResults); } catch (e) { $.export("

what does this mean in pipedream firebase trigger? when do i receive events ?
polling is every 2 hours and query is
{ select: { fields: }, from: [ { collectionId: “collection-name”, allDescendants: false, }, ], where: { fieldFilter: { field: { fieldPath: “start” }, op: “GREATER_THAN”, value: { stringValue: currentDayStart.toISOString(), }, }, }, orderBy: [{ field: { fieldPath: “start” }, direction: “DESCENDING” }], limit: 1, }

In Pipedream, when you configure a Firebase trigger with a polling interval of every 2 hours and a query like the one you’ve described, it means the following: - Polling Interval: Your trigger will check for new events in your specified Firebase collection every 2 hours. This is a timer-based polling mechanism where Pipedream schedules the trigger to run at the specified interval to fetch new data. - Query Explanation: The query you’ve provided is structured to fetch documents from a specific Firebase collection named “collection-name”. It filters documents based on the start field, looking for documents where the start field’s value is greater than the currentDayStart.toISOString() value you’ve provided. This means it’s targeting documents that have a start date and time after the specified currentDayStart value. The query also orders the results in descending order by the start field and limits the results to 1 document. This setup is likely designed to fetch the most recent document (or event) that meets the criteria. - When You Receive Events: Given this setup, you will receive events in your Pipedream workflow every 2 hours, but only if there are new documents in your Firebase collection that match the query criteria since the last poll. If no new documents match the criteria, no event will be emitted for that polling cycle. This trigger mechanism allows you to automate workflows based on new or updated data in your Firebase collection, with the workflow being triggered at the specified intervals if matching documents are found.