Skip to content

Instantly share code, notes, and snippets.

@cybo42
Last active September 21, 2020 15:35
Show Gist options
  • Save cybo42/5c92ed38fae897149c67 to your computer and use it in GitHub Desktop.
Save cybo42/5c92ed38fae897149c67 to your computer and use it in GitHub Desktop.
fix "[object Object] does not exist" issue in node-mongo when using non ObjectId keys
//
var _open = function(self, options, callback) {
var collection = self.collection();
// Create the query
var query = self.referenceBy == REFERENCE_BY_ID ? {_id:self.fileId} : {filename:self.filename};
query = null == self.fileId && self.filename == null ? null : query;
options.readPreference = self.readPreference;
// Fetch the chunks
if(query != null) {
collection.find(query, options, function(err, cursor) {
if(err) return error(err);
// Fetch the file
cursor.nextObject(function(err, doc) {
if(err) return error(err);
// Check if the collection for the files exists otherwise prepare the new one
if(doc != null) {
self.fileId = doc._id;
// Prefer a new filename over the existing one if this is a write
self.filename = ((self.mode == 'r') || (self.filename == undefined)) ? doc.filename : self.filename;
self.contentType = doc.contentType;
self.internalChunkSize = doc.chunkSize;
self.uploadDate = doc.uploadDate;
self.aliases = doc.aliases;
self.length = doc.length;
self.metadata = doc.metadata;
self.internalMd5 = doc.md5;
} else if (self.mode != 'r') {
self.fileId = self.fileId == null ? new ObjectID() : self.fileId;
self.contentType = exports.GridStore.DEFAULT_CONTENT_TYPE;
self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize;
self.length = 0;
} else {
self.length = 0;
var txtId = self.fileId instanceof ObjectID ? self.fileId.toHexString() : JSON.stringify(self.fileId);
return error(new Error((self.referenceBy == REFERENCE_BY_ID ? txtId : self.filename) + " does not exist", self));
}
Line 203 in mongodb@1.4.7
var _open = function(self, options, callback) {
var collection = self.collection();
// Create the query
var query = self.referenceBy == REFERENCE_BY_ID ? {_id:self.fileId} : {filename:self.filename};
query = null == self.fileId && self.filename == null ? null : query;
options.readPreference = self.readPreference;
// Fetch the chunks
if(query != null) {
collection.find(query, options, function(err, cursor) {
if(err) return error(err);
// Fetch the file
cursor.nextObject(function(err, doc) {
if(err) return error(err);
// Check if the collection for the files exists otherwise prepare the new one
if(doc != null) {
self.fileId = doc._id;
// Prefer a new filename over the existing one if this is a write
self.filename = ((self.mode == 'r') || (self.filename == undefined)) ? doc.filename : self.filename;
self.contentType = doc.contentType;
self.internalChunkSize = doc.chunkSize;
self.uploadDate = doc.uploadDate;
self.aliases = doc.aliases;
self.length = doc.length;
self.metadata = doc.metadata;
self.internalMd5 = doc.md5;
} else if (self.mode != 'r') {
self.fileId = self.fileId == null ? new ObjectID() : self.fileId;
self.contentType = exports.GridStore.DEFAULT_CONTENT_TYPE;
self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize;
self.length = 0;
} else {
self.length = 0;
var txtId = self.fileId instanceof ObjectID ? self.fileId.toHexString() : self.fileId;
return error(new Error((self.referenceBy == REFERENCE_BY_ID ? txtId : self.filename) + " does not exist", self));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment