awapps / mongration

MongoDB migration framework
MIT License
85 stars 24 forks source link

accessing MongoDb client within migrations doesn't work as expected #32

Closed tw-epenkman closed 6 years ago

tw-epenkman commented 8 years ago

In particular I'm trying to use an ObjectId in a find:

// in a migration script
const ObjectId = require('mongodb').ObjectId;
//  ... 
up: function (db, cb) {
    db.collection('persons').findOne({_id: ObjectId('580e1102ac5378af70f4d9f4'}, (err,response)=>{
        //error!
    })
}

Error!

TypeError: Argument must be a string
    at TypeError (native)
    at Buffer.write (buffer.js:763:21)
    at serializeObjectId (./node_modules/mongration/node_modules/bson/lib/bson/parser/serializer.js:242:10)
    at serializeInto (./node_modules/mongration/node_modules/bson/lib/bson/parser/serializer.js:699:17)
    at serializeObject (./node_modules/mongration/node_modules/bson/lib/bson/parser/serializer.js:280:18)
    at serializeInto (./node_modules/mongration/node_modules/bson/lib/bson/parser/serializer.js:705:17)
    at serialize (./node_modules/mongration/node_modules/bson/lib/bson/bson.js:47:27)
    at Query.toBin (./node_modules/mongration/node_modules/mongodb-core/lib/connection/commands.js:146:25)
    at Cursor._find (./node_modules/mongration/node_modules/mongodb-core/lib/cursor.js:263:30)
    at nextFunction (./node_modules/mongration/node_modules/mongodb-core/lib/cursor.js:576:10)

I fixed it locally by doing this

const ObjectId = require('mongration/node_modules/mongodb').ObjectId;

It looks very similar to this issue in sails

https://stackoverflow.com/questions/38603529/argument-must-be-a-string-in-nodejs/38606392#comment67911586_38606392

eberhara commented 8 years ago

Hey there! Thanks for finding that. Would you like to propose a fix for that, or your workaround is enough?

Thanks!!

wmartins commented 6 years ago

@epenkman closing due to lack of activity here. If you need, you can reopen it!