Roronoa Zoro
Roronoa Zoro

Reputation: 1

Fetching user documents... Failed to parse JSON payload: TooDeep (ESP8266)

first I want to iterate to my users which document will match to my predefined parcelBoxNo., after that if it matched then it will go to orders, and retrieve the data/info of the orders trackingNo. and qrcodedata. I also want that in orders it will iterate again cause i need to integrate this to my keypad and qrcode scanner hardware and compare that data to the database data.'

but everytime i upload the output is: Fetching user documents... Failed to parse JSON payload: TooDeep

void retrieveAndDisplayData() {
  const int predefinedParcelBox = 1; // Define the predefined parcel box number to compare
  const char* documentPath = "users"; // Path to the collection

  // Construct Firestore query to filter documents
  FirebaseJson query;
  query.set("structuredQuery.where.fieldFilter.field.fieldPath", "aParcelBoxNo");
  query.set("structuredQuery.where.fieldFilter.op", "EQUAL");
  query.set("structuredQuery.where.fieldFilter.value.integerValue", predefinedParcelBox);

  // Retrieve filtered documents from Firestore
  Serial.print("Fetching user documents... ");
  if (Firebase.Firestore.runQuery(&fbdo, FIREBASE_PROJECT_ID, "", documentPath, &query)) {
    // Successfully retrieved filtered documents
    String payload = fbdo.payload(); // Get the payload as a string

    // Parse the payload as a JSON array of documents
    DynamicJsonDocument jsonDoc(2048); // Adjust capacity based on expected payload size
    DeserializationError jsonError = deserializeJson(jsonDoc, payload);

    if (jsonError) {
      Serial.print("Failed to parse JSON payload: ");
      Serial.println(jsonError.c_str());
      return;
    }

    // Check if the payload contains documents
    if (!jsonDoc.containsKey("documents")) {
      Serial.println("No documents found matching the query.");
      return;
    }

    // Process the retrieved documents
    JsonArray documents = jsonDoc["documents"].as<JsonArray>();
    for (JsonObject doc : documents) {
      // Extract specific fields from each document
      if (doc.containsKey("fields")) {
        JsonObject fields = doc["fields"];

        // Access specific field values (e.g., trackingNumber, qrCodeData)
        if (fields.containsKey("trackingNumber") && fields.containsKey("qrCodeData")) {
          String trackingNumber = fields["trackingNumber"]["stringValue"].as<String>();
          String qrCodeData = fields["qrCodeData"]["stringValue"].as<String>();

          // Process the retrieved data
          Serial.print("Tracking Number: ");
          Serial.println(trackingNumber);
          Serial.print("QR Code Data: ");
          Serial.println(qrCodeData);

          // Optionally perform additional actions based on the retrieved data
          // E.g., match trackingNumber with predefined value, trigger actions
        }
      }
    }
  } else {
    // Failed to retrieve documents or run query
    Serial.print("Firestore query error: ");
    Serial.println(fbdo.errorReason());
  }
}



void loop() {
  if (millis() - lastDataMillis >= interval || lastDataMillis == 0) {
    lastDataMillis = millis();
    retrieveAndDisplayData();
  }

  delay(1000); // Add a short delay to prevent continuous looping
}

Upvotes: 0

Views: 32

Answers (0)

Related Questions