is there a way to access the mongodb during the Meteor.startup()
I need to insert/update a document in a collection during the Meteor.startup()
I tried:
// https://www.npmjs.com/package/mongodb
const MongoClient = require('mongodb').MongoClient;
// await may be missing but when i use await, i get a reserved keyword error
const mongodb = MongoClient.connect(process.env.MONGO_URL)
mongodb.collection('collection').insertOne(objectData)
// Error
// TypeError: mongodb.collection is not a function
// at Meteor.startup (server/migrations.js:1212:23)
and
const col = Mongo.Collection('collection');
// Error
// TypeError: this._maybeSetUpReplication is not a function
// at Object.Collection [as _CollectionConstructor] (packages/mongo/collection.js:109:8)
Anyone got a solution?
The reason you are having the error is not because you are accessing mongodb in the
startupmethod, it's because theMongoClient.connectmethod is asynchronous, consequently, you can only access your mongodb collections after theconnectmethod resolves. You should try something like this instead:For more explanation on connecting via MongoClient, check here.