OpenSourceHelpCommunity / Questions

To add your question raise an issue in this repository.
MIT License
27 stars 47 forks source link

Firebase cloud functions "error": "\"length\" is outside of buffer bounds" #519

Open Mapenergo opened 1 month ago

Mapenergo commented 1 month ago

Working on MacOS v 14.6.1 Apple M2 node 22.7.0 npm 10.8.2

I passed through the lesson https://firebase.google.com/docs/functions/get-started?hl=en&gen=2nd

And encountered an error ""length" is outside of buffer bounds" after running the function: http://127.0.0.1:5001/povarcloud/us-central1/addmessage

How can I fix it?

Here is the code:

const {logger} = require("firebase-functions");
const {onRequest} = require("firebase-functions/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");

// The Firebase Admin SDK to access Firestore.
const {initializeApp} = require("firebase-admin/app");
const {getFirestore} = require("firebase-admin/firestore");

initializeApp();

// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
exports.addmessage = onRequest(async (req, res) => {
    const original = req.query.text;

    if (!original) {
      res.status(400).json({error: "Text parameter is required"});
      return;
    }

    try {
      const writeResult = await getFirestore()
          .collection("messages")
          .add({original: original});
      res.json({result: `Message with ID: ${writeResult.id} added.`});
    } catch (error) {
      res.status(500).json({error: error.message});
    }
  });

UPD: I've fixed it by installing Node 20 instead of Node 22.