Error: RangeError [ERR_BUFFER_OUT_OF_BOUNDS] when Fetching Document from Firestore

I’m encountering an error while trying to fetch a user profile document from Firestore using Node.js. Here’s a snippet of my code:

const profileRef = db.collection("profiles").doc(uid);
const profileSnapshot = await profileRef.get();

if (!profileSnapshot.exists) {
  return res.status(404).send({ message: "Profile not found" });
}

const profileInfo = profileSnapshot.data();

When I run my server, I receive the following error:

Error stack: RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: "length" is outside of buffer bounds
    at proto.utf8Write (node:internal/buffer:1066:13)
    at Op.writeStringBuffer [as fn] (/Users/fao/Documents/spotters-api/node_modules/protobufjs/src/writer_buffer.js:61:13)
    at BufferWriter.finish (/Users/fao/Documents/spotters-api/node_modules/protobufjs/src/writer.js:453:14)
    at /Users/fao/Documents/spotters-api/node_modules/@grpc/proto-loader/build/src/index.js:177:109
    at Array.map (<anonymous>)
    at createPackageDefinition (/Users/fao/Documents/spotters-api/node_modules/@grpc/proto-loader/build/src/index.js:177:39)
    at Object.fromJSON (/Users/fao/Documents/spotters-api/node_modules/@grpc/proto-loader/build/src/index.js:230:12)
    at GrpcClient.loadProtoJSON (/Users/fao/Documents/spotters-api/node_modules/google-gax/build/src/grpc.js:228:51)
    at new LocationsClient (/Users/fao/Documents/spotters-api/node_modules/google-gax/build/src/locationService.js:118:32)
    at new FirestoreClient (/Users/fao/Documents/spotters-api/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:136:32)
Caused by: Error
    at /Users/fao/Documents/spotters-api/node_modules/@google-cloud/firestore/build/src/index.js:1042:27
    at DisabledTraceUtil.startActiveSpan (/Users/fao/Documents/spotters-api/node_modules/@google-cloud/firestore/build/src/telemetry/disabled-trace-util.js:16:16)
    at Firestore.getAll (/Users/fao/Documents/spotters-api/node_modules/@google-cloud/firestore/build/src/index.js:1033:32)
    at /Users/fao/Documents/spotters-api/node_modules/@google-cloud/firestore/build/src/reference/document-reference.js:182:36
    at DisabledTraceUtil.startActiveSpan (/Users/fao/Documents/spotters-api/node_modules/@google-cloud/firestore/build/src/telemetry/disabled-trace-util.js:16:16)
    at DocumentReference.get (/Users/fao/Documents/spotters-api/node_modules/@google-cloud/firestore/build/src/reference/document-reference.js:181:43)
    at getProfilePageModel (/Users/fao/Documents/spotters-api/src/controllers/dataBaseController.js:38:48)
    at Layer.handle [as handle_request] (/Users/fao/Documents/spotters-api/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/fao/Documents/spotters-api/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/fao/Documents/spotters-api/node_modules/express/lib/router/route.js:119:3)
(node:54380) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

Environment Details:

Node.js version: v22.7.0

package.json:

{
  "name": "spotters-api",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "@google-cloud/firestore": "^7.10.0",
    "dotenv": "^16.4.5",
    "express": "^4.21.0",
    "firebase-admin": "^12.5.0"
  }
}

Steps I’ve Taken:

Verified that the Firestore document exists for the given UID.
Ensured that my Firestore rules allow read access to the profiles collection.
Updated all dependencies to their latest versions.
Additional Information:

The error occurs on the line where I call profileRef.get().
I’m using Express.js for my server.
Can anyone help me understand what might be causing this error and how to resolve it?

4

If you’re encountering the following error while accessing Firestore with Node.js version 22.7.0:

RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: "length" is outside of buffer bounds
    at proto.utf8Write (node:internal/buffer:1066:13)
    at Op.writeStringBuffer [as fn] (node_modules/protobufjs/src/writer_buffer.js:61:13)

This is a known issue with Node.js version 22.7.0. The problem doesn’t occur in earlier versions like Node.js 22.6.0 or 22.4.0.

Solution:
Upgrade to Node.js 22.8.0. The issue has been fixed in this release.

Check this github issue for more detail:

[Firestore] RangeError thrown on Node version 22.7.0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật