Unable to remove Repeatable Job From Bull Queue Node Js - javascript

When i tried using this method - removeRepeatableByKey I am getting removeRepeatableByKey is not a function error.
queue_1.taskQueue.removeRepeatableByKey is not a function
I am not able to remove my repeatable job bytaskQueue.removeRepeatable('task', { cron: '0 47 6 * * 4' }); this too
let jobOptions = {
priority: queue_options.priority,
repeat: { cron: '0 47 6 * * 4'},
attempts: 3,
removeOnComplete: true,
jobId: queue_options.job_id,
backoff: 3,
timeout: 60000,
};
taskQueue.add('task', queue_options.data, jobOptions);
JSON of the JOB:
{ id: "repeat:09854c8042eced1337a7d8eec9357528:1552526220000", name: "task", data: { eventName: "test", parameters: [ { my_JSON }, { my_JSON } ] }, opts: { repeat: { count: 1, cron: "0 47 6 * * 4", jobId: "myJobId" }, jobId: "repeat:09854c8042eced1337a7d8eec9357528:1552526220000", delay: 603096068, timestamp: 1551923123932, prevMillis: 1552526220000, priority: 1, attempts: 3, removeOnComplete: true, backoff: { type: "fixed", delay: 3 }, timeout: 60000 }, progress: 0, delay: 603096068, timestamp: 1551923123932, attemptsMade: 0, stacktrace: [ ], returnvalue: null, finishedOn: null, processedOn: null }

I am able to remove the repeatable job now. The problem was i am adding the job to the queue with:
let jobOptions = {
priority: queue_options.priority,
repeat: { cron: '0 47 6 * * 4' },
attempts: 3,
removeOnComplete: true,
jobId: queue_options.job_id,
backoff: 3,
timeout: 60000,
};
taskQueue.add('task', queue_options.data, jobOptions);
And trying to remove the job like:
let job = await taskQueue.removeRepeatable('task', {cron : '0 47 6 * * 4'});
I fixed it by passing the jobOptions and i was able to successfully remove the job from the queue.
let job = await taskQueue.removeRepeatable('task', jobOptions);

Related

object property returns undefined even though it exists JavaScript

I'm trying to make a clone system of Pokemon, using objects. Using PokeAPI, I get all the pokemon's evolutions, and if the pokemon has more than 1 evolution, (I.E Kirlia), It pushes all the evolution object properties the Pokemon.evolutions, which is an array of these evolution objects. When I try to compile the code It crashes saying: TypeError: Cannot read properties of undefined (reading 'species'), so I think that it means because I typed the object wrong, but That's not it, If I console.log data.evolves_to[i].species.name (the line causing the app to crash), I get the pokemon name, and it's correct! and then it crashes on the same line! It gets logged in the console and then crash. What's going on? Here's the code for context:
async getRandomPokemon() {
const rawData: PokemonData[] | PokemonSpecies[] = [];
const id = utils.rng({ min: 1, max: 898 });
rawData[0] = await api.getPokemonById(id);
rawData[1] = await api.getPokemonSpeciesById(id);
const pokemon: Pokemon = {
name: rawData[0].name,
level: utils.rng({ min: 1, max: 10 }),
gender: utils.pick({ array: ["male", "female"] }),
exp: 1,
stats: {
hp: rawData[0].stats[0].base_stat,
atk: rawData[0].stats[1].base_stat,
def: rawData[0].stats[2].base_stat,
spa: rawData[0].stats[3].base_stat,
spd: rawData[0].stats[4].base_stat,
spe: rawData[0].stats[5].base_stat,
IVs: {
hp: utils.rng({ min: 0, max: 31 }),
atk: utils.rng({ min: 0, max: 31 }),
def: utils.rng({ min: 0, max: 31 }),
spa: utils.rng({ min: 0, max: 31 }),
spd: utils.rng({ min: 0, max: 31 }),
spe: utils.rng({ min: 0, max: 31 }),
},
EVs: {
hp: utils.rng({ min: 0, max: 252 }),
atk: utils.rng({ min: 0, max: 252 }),
def: utils.rng({ min: 0, max: 252 }),
spa: utils.rng({ min: 0, max: 252 }),
spd: utils.rng({ min: 0, max: 252 }),
spe: utils.rng({ min: 0, max: 252 }),
},
},
moves: [],
evolutions: [],
aesthetics: {
sprite: rawData[0].sprites.front_default,
color: rawData[1].color.name,
},
rarity: {
isLegendary: rawData[1].is_legendary,
isMythical: rawData[1].is_mythical,
catchRate: rawData[1].capture_rate,
},
};
const json = await axios(rawData[1].evolution_chain.url);
let data = json.data.chain;
for (let i = 0; i < data.evolves_to.length; i++) {
const one: boolean = data.evolves_to[i].evolves_to.length === 0;
pokemon.evolutions.push({
first: data.species.name,
second: {
name: data.evolves_to[i].species.name,
mode: {
name: data.evolves_to[i].evolution_details[0].trigger.name,
gender: data.evolves_to[i].evolution_details[0].gender,
level: data.evolves_to[i].evolution_details[0].min_level,
item: data.evolves_to[i].evolution_details[0].item
? data.evolves_to[i].evolution_details[0].item.name
: null,
happiness: data.evolves_to[i].evolution_details[0].min_happiness,
move: data.evolves_to[i].evolution_details[0].known_move,
},
},
third: one
? null
: {
name: one ? null : data.evolves_to[i].evolves_to[0].species.name,
mode: {
name: data.evolves_to[i].evolves_to[0].evolution_details[0]
.trigger.name,
gender:
data.evolves_to[i].evolves_to[0].evolution_details[0].gender,
level:
data.evolves_to[i].evolves_to[0].evolution_details[0]
.min_level,
item: data.evolves_to[i].evolves_to[0].evolution_details[0].item
? data.evolves_to[i].evolves_to[0].evolution_details[0].item
.name
: null,
happiness:
data.evolves_to[i].evolves_to[0].evolution_details[0]
.min_happiness,
move: data.evolves_to[i].evolves_to[0].evolution_details[0]
.known_move,
},
},
});
if (data.evolves_to[0].evolves_to.length > 1) {
for (let i = 0; i < data.evolves_to[0].evolves_to.length; i++) {
console.log(data.evolves_to[i].species.name);
pokemon.evolutions.push({
first: data.species.name,
second: {
name: data.evolves_to[i].species.name,
mode: {
name: data.evolves_to[i].evolution_details[0].trigger.name,
gender: data.evolves_to[i].evolution_details[0].gender,
level: data.evolves_to[i].evolution_details[0].min_level,
item: data.evolves_to[i].evolution_details[0].item
? data.evolves_to[i].evolution_details[0].item.name
: null,
happiness:
data.evolves_to[i].evolution_details[0].min_happiness,
move: data.evolves_to[i].evolution_details[0].known_move,
},
},
third: one
? null
: {
name: one
? null
: data.evolves_to[i].evolves_to[0].species.name,
mode: {
name: data.evolves_to[i].evolves_to[0].evolution_details[0]
.trigger.name,
gender:
data.evolves_to[i].evolves_to[0].evolution_details[0]
.gender,
level:
data.evolves_to[i].evolves_to[0].evolution_details[0]
.min_level,
item: data.evolves_to[i].evolves_to[0].evolution_details[0]
.item
? data.evolves_to[i].evolves_to[0].evolution_details[0]
.item.name
: null,
happiness:
data.evolves_to[i].evolves_to[0].evolution_details[0]
.min_happiness,
move: data.evolves_to[i].evolves_to[0].evolution_details[0]
.known_move,
},
},
});
}
}
}
return pokemon;
},
The line that crashes the app is console.log(data.evolves_to[i].species.name); (106, almost at the bottom). The strange thing is that data.evolves_to[i].species.name is already in use when getting the first evolution, but crashes when trying to get the alternate evolutions.
The for loop right above your console.log is causing the problem
for (let i = 0; i < data.evolves_to[0].evolves_to.length; i++)
You have "data.evolves_to[0].evolves_to.length" when I think you only need "data.evolves_to.length".
This would happen if you have fewer objects in "data.evolves_to" than in "data.evolves_to[0].evolves_to", whatever that happens to be.
You can try to find errors like by stepping through the program using the browsers development console.
It's obviously a copy & paste error, now that we know what to look for.

Mongoose `findById` not returning expected document

When I find a document using the following line:
const user = await User.findById(req.user.id).populate('input')
And then I console.log user, I can see the document that fulfills the query.
However, when I add .updateOne({ formResponded: '1' }); to the previous line like so:
const user = await User.findById(req.user.id).populate('input').updateOne({ formResponded: '1'});
and I console.log user I do not get any record but instead an object that looks like this:
{ n: 1,
nModified: 1,
opTime: {
ts: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1635212112 },
t: 41
},
electionId: 7fffffff0000000000000029,
ok: 1,
'$clusterTime': {
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1635212112 },
signature: { hash: [Binary], keyId: [Long] }
},
operationTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1635212112 }
}
Why does this happen?
Fixed:
const user = await User.findByIdAndUpdate({ _id: req.user.id }, { formResponded: '1' }).populate('input')

Issue finding mongoose ObjectID in array of strings representing ObjectIDs

I need to find the index of the mongoose objectID in an array like this:
[ { _id: 58676b0a27b3782b92066ab6, score: 0 },
{ _id: 58676aca27b3782b92066ab4, score: 3 },
{ _id: 58676aef27b3782b92066ab5, score: 0 }]
The model I am using to compare is a mongoose schema with the following data:
{_id: 5868d41d27b3782b92066ac5,
updatedAt: 2017-01-01T21:38:30.070Z,
createdAt: 2017-01-01T10:04:13.413Z,
recurrence: 'once only',
end: 2017-01-02T00:00:00.000Z,
title: 'Go to bed without fuss / coming down',
_user: 58676aca27b3782b92066ab4,
__v: 0,
includeInCalc: true,
result: { money: 0, points: 4 },
active: false,
pocketmoney: 0,
goals: [],
pointsawarded: { poorly: 2, ok: 3, well: 4 },
blankUser: false }
I am trying to find the index of the model._user in the array above using the following:
var isIndex = individualScores.map(function(is) {return is._id; }).indexOf(taskList[i]._user);
Where individualScores is the original array and taskList[i] is the task model. However, this always returns -1. It never finds the correct _id in the array.
I guess your problem is related to how _id are returned by your query
If you get _id as String, your code should work, check the snippet below
But if instead, you get ObjectsIds, you have to cast them to String first
var individualScores = [
{ _id: "58676b0a27b3782b92066ab6", score: 0 },
{ _id: "58676aca27b3782b92066ab4", score: 3 },
{ _id: "58676aef27b3782b92066ab5", score: 0 }
]
var task = {
_id: "5868d41d27b3782b92066ac5",
updatedAt: new Date("2017-01-01T21:38:30.070Z"),
createdAt: new Date("2017-01-01T10:04:13.413Z"),
recurrence: 'once only',
end: new Date("2017-01-02T00:00:00.000Z"),
title: 'Go to bed without fuss / coming down',
_user: "58676aca27b3782b92066ab4",
__v: 0,
includeInCalc: true,
result: { money: 0, points: 4 },
active: false,
pocketmoney: 0,
goals: [],
pointsawarded: { poorly: 2, ok: 3, well: 4 },
blankUser: false
}
var isIndex = individualScores.map(
function(is) {
return is._id;
})
.indexOf(task._user);
console.log(isIndex)
I think your process should working well that you are using just only need to convert 'objectID' to String to compare. Convert using .toString() for both of _id and _user.
like bellow:
var isIndex = individualScores.map(function(is) {
return is._id.toString();
}).indexOf(taskList[i]._user.toString());

mongodb search query - returns all results (non matching ones too)

This is a sample from MEAN stack website,
I require the query to return parameters that match ' exactly ' with the input.
Please view the image attached to understand the issue better.
Search Query Function
Any hint on this issue ? (I'm a beginner so please elaborate a little)
-TIA :)
Input for the search from the browser
{ body: { hp: 1, length: 1, diameter: 1, voltage: 1 } }
// mongo schema
var CableSchema = new schema({
body : {
"hp": {
type: Number
},
"length": {
type: Number
},
"diameter": {
type: Number
},
"voltage": {
type: Number
},
"cost": {
type: Number
},
"type": {
type: String,
default: "Cable"
}
}
});
-----------------------------------------------------------
// Result from Search Query obtained in console
[ { body:
{ type: 'Cable',
cost: 1,
voltage: 1,
diameter: 1,
length: 1,
hp: 1 },
__v: 0,
_id: 5820246086d42a3c269ad9f2 },
{ body:
{ type: 'Cable',
cost: 2,
voltage: 2,
diameter: 2,
length: 2,
hp: 2 },
__v: 0,
_id: 5820249086d42a3c269ad9f3 } ]`
The keys 'hp','length' etc are inside the body object of cable schema. So to refer 'hp' use 'body.hp' in query
Change your query to
var query = Cable.find({'body.hp' : parseInt(reqHp) , 'body.length' : parseInt(reqLen),
'body.diameter' : parseInt(reqDia) ,'body.voltage' : parseInt(reqVol)})
The confusion was with assignment - 1body was from req body and the other one was from '2body' schema
I needed to use 1body.2body to reach inside for the data.
var reqHP = req.body.body.hp;
var reqLen = req.body.body.length;
var reqDia = req.body.body.diameter;
var reqVol = req.body.body.voltage;
var reqCost = req.body.body.cost;

UnderscoreJS wont extend object

I am trying to merge 2 objects using underscore. The destination object is a mongoose model but I have applied lean() to it to make it return a javascript object rather than a mongo document.
model.find({}).lean().exec(function (error, object) {});
I then try extending using underscore
_.extend(object, source);
But it only returns the source object.
I have tried with simple objects to test and those worked fine, so I am assuming it has something to do with mongoose?
The simple objects that worked were:
{foo:'foo'},{bar:'bar'}
And the objects that I am trying to merge but haven't been able to are:
{
_id: 526540eaa77883d815000029,
name: 'House',
description: '',
type: 'residential',
cost: 100,
buildTime: 5,
resources: { produces: [], required: { wood: 5 } },
population: { provides: 10, required: 0 },
requires: [],
maxLevel: 5,
upgrades:
{ '2': { resourceMultiplier: 1.2, cost: 150, time: 5 },
'3': { resourceMultiplier: 1.5, cost: 200, time: 7 },
'4': { resourceMultiplier: 2, cost: 300, time: 10 },
'5': { resourceMultiplier: 2.5, cost: 500, time: 15 } },
scale: { x: 1, y: 1, z: 1 }
}
{
empireId: '52654578a4eff60000000001',
buildingId: '526540eaa77883d815000029',
level: 1,
isComplete: false,
isUpgrading: false,
gridId: '175|0|125',
started: 1382442513823,
_id: 526666113fccae68be000003,
__v: 0
}
Anyone come across this before or know where I am going wrong?
Well I am stupid. The source object was gotten from another mongoose query at the top of the file, this one was an instance of mongoose.Document and therefore couldn't be changed. I added lean() to it to make it return a javascript object and it's all working now.

Categories