Skip to content

Commit 22a4628

Browse files
authored
fix(GridFS): fix TypeError: doc.data.length is not a function (#1570)
This is a port of #1555 to 3.0.0 branch. When using the promoteBuffers connect option, `doc.data` is already a node.js Buffer. So trying to call `length()` like for `BSON.Binary` fails. Fixed simply by checking `Buffer.isBuffer(doc.data)` type.
1 parent 4ac475c commit 22a4628

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

lib/gridfs-stream/download.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -207,29 +207,27 @@ function doRead(_this) {
207207
return __handleError(_this, new Error(errmsg));
208208
}
209209

210-
if (doc.data.length() !== expectedLength) {
210+
var buf = Buffer.isBuffer(doc.data) ? doc.data : doc.data.buffer;
211+
212+
if (buf.length !== expectedLength) {
211213
if (bytesRemaining <= 0) {
212214
errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n;
213215
return __handleError(_this, new Error(errmsg));
214216
}
215217

216218
errmsg =
217-
'ChunkIsWrongSize: Got unexpected length: ' +
218-
doc.data.length() +
219-
', expected: ' +
220-
expectedLength;
219+
'ChunkIsWrongSize: Got unexpected length: ' + buf.length + ', expected: ' + expectedLength;
221220
return __handleError(_this, new Error(errmsg));
222221
}
223222

224-
_this.s.bytesRead += doc.data.length();
223+
_this.s.bytesRead += buf.length;
225224

226-
if (doc.data.buffer.length === 0) {
225+
if (buf.length === 0) {
227226
return _this.push(null);
228227
}
229228

230229
var sliceStart = null;
231230
var sliceEnd = null;
232-
var buf = doc.data.buffer;
233231

234232
if (_this.s.bytesToSkip != null) {
235233
sliceStart = _this.s.bytesToSkip;
@@ -241,7 +239,7 @@ function doRead(_this) {
241239
}
242240

243241
// If the remaining amount of data left is < chunkSize read the right amount of data
244-
if (_this.s.options.end && _this.s.options.end - _this.s.bytesToSkip < doc.data.length()) {
242+
if (_this.s.options.end && _this.s.options.end - _this.s.bytesToSkip < buf.length) {
245243
sliceEnd = _this.s.options.end - _this.s.bytesToSkip;
246244
}
247245

0 commit comments

Comments
 (0)