i want to update my user login datetime using background job, i'm using mongodb/mongoose as primary database but when i call methods of my model inside of process body it said for example : TypeError: User.findOneAndUpdate is not a function
const { opts } = require('./redisConnection');
const Queue = require('bull');
const User = require('../../models/user.model');
const lastLoginQueue = new Queue('last-login', opts);
lastLoginQueue.process(async (job) => {
console.log('Received ', job.data.userId);
const { userId } = job.data;
const user = await User.findOneAndUpdate(
{ _id: userId },
{
$currentDate: {
lastLogin: true,
},
},
);
return Promise.resolve();
});
when run above code it said:
[ 'TypeError: User.findOneAndUpdate is not a function\n
maybe because methods of mongoose models is virtual or something else happend.
I am going to close this issue since it is unrelated to bull.
it's related to bull, anyway i found the solution. somehow methods of mongoose models not working inside job.process i used mongodb native driver and it's worked.
Thank you for this issue!! I also ran into this trouble and was debugging forever trying to use async/await and also trying callback and nothing worked!
once i switched to the native drivers, it worked
Same Case Here. My mongoose model imported is set as undefined. Any other method than using mongodb native package?
Got it, it was because of circular dependency somewhere in my code. If someone else faces the same situation.
thanks, just happened to me
There's no way to use mongoose? only the native driver?
Same Case Here. My mongoose model imported is set as undefined. Any other method than using mongodb native package?
So you are saying you were able to use Mongoose to query the database?
@RizaHKhan As far as I know this only happens using external sandboxed processors. And the cause must be due to some exotic thing in mongoose, bull is not doing anything special just spawning a process and running the processor inside it.
@manast maybe mongoose need some time to initialize the connection and models, I'm not using Bull right now but if had time will checking some ways like passing mongoose as a parameter to the process method
@manast I found that my socket-io instance is not also shared to the spawned process. This might be the same problem with mongoose.
Globals, Variables defined in the main process does not pass to the child(even if they cached with require call in Node, its not initialized for sandbox processes)
@kasvith yes, thats kind of the point of running a separate process so that you get complete isolation. But I am not sure that is the problem with mongoose though since as reported here the poster is not using a external process, just a callback, which is nothing special so I don't understand why it does not work, however I have seen very strange behaviours with mongoose in the past.
My issue was resolved simply by reinitializing Mongoose under the worker process. Hope that helps anyone else that might have this issue.
PS: I found the solution based on a response by @manast on a similar issue.
My issue was resolved simply by reinitializing Mongoose under the worker process. Hope that helps anyone else that might have this issue.
PS: I found the solution based on a response by @manast on a similar issue.
Could you please be more specific on what you did exactly and maybe link to the similar issue you're referring to?
@hardnold
I'm writing an article about using bull with mongodb, tomorrow in my timezone I can publish it. But until then you can take look the repo for that article and how I used mongoos with bull.
Cheers 馃嵒
https://github.com/miladr0/bull-mongo
link to the tutorial: https://medium.com/better-programming/message-queue-using-bull-redis-and-mongodb-in-node-js-d7dedaa426ea
once again tnx for your awesome library @manast
link to the tutorial: https://medium.com/better-programming/message-queue-using-bull-redis-and-mongodb-in-node-js-d7dedaa426ea
once again tnx for your awesome library @manast
Thanks. Nice Article. Didn't help to fix my problem though 馃槈 (I didn't get the OP's described Type Error, instead just nothing happened).
What actually solved my Problem was to open a new DB-Connection for the processor (which is in a separate File).
Most helpful comment
it's related to bull, anyway i found the solution. somehow methods of mongoose models not working inside job.process i used mongodb native driver and it's worked.