Skip to content

Commit 811de0c

Browse files
guymguymdaprahamian
authored andcommitted
fix(GridFS): fix TypeError: doc.data.length is not a function
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 f4e85c6 commit 811de0c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lib/gridfs-stream/download.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,27 @@ function doRead(_this) {
209209
return __handleError(_this, new Error(errmsg));
210210
}
211211

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

218220
errmsg = 'ChunkIsWrongSize: Got unexpected length: ' +
219-
doc.data.length() + ', expected: ' + expectedLength;
221+
buf.length + ', expected: ' + expectedLength;
220222
return __handleError(_this, new Error(errmsg));
221223
}
222224

223-
_this.s.bytesRead += doc.data.length();
225+
_this.s.bytesRead += buf.length;
224226

225-
if (doc.data.buffer.length === 0) {
227+
if (buf.length === 0) {
226228
return _this.push(null);
227229
}
228230

229231
var sliceStart = null;
230232
var sliceEnd = null;
231-
var buf = doc.data.buffer;
232233

233234
if (_this.s.bytesToSkip != null) {
234235
sliceStart = _this.s.bytesToSkip;
@@ -241,7 +242,7 @@ function doRead(_this) {
241242

242243
// If the remaining amount of data left is < chunkSize read the right amount of data
243244
if (_this.s.options.end && (
244-
(_this.s.options.end - _this.s.bytesToSkip) < doc.data.length()
245+
(_this.s.options.end - _this.s.bytesToSkip) < buf.length
245246
)) {
246247
sliceEnd = (_this.s.options.end - _this.s.bytesToSkip);
247248
}

0 commit comments

Comments
 (0)