Find a specific object and its Index from an Array of Objects through ObjectID

Newbie_developer

I store value in db like this

PendingTasks: [

{ uniqueId :1212322323
taskId : ObjectId(62863e2ed0d1bc5asas9b04711)
_id : 6289100e9823422047b7a},

{ uniqueId :1212322323
taskId : ObjectId(62863e2ed0d1bc5asas9b04711)
_id : 6289100e9823422047b7a},

{ uniqueId :1212322323
taskId : ObjectId(62863e2ed0d1bc5asas9b04341)
_id : 6289100e9823422047b7a},

],

I make put request like this

router.put('/:companyId/:uniqueId/:taskId',........

From this request, I get the taskId through req.params.taskId and then search pending tasks and find the first object that matches this condition (notice there will be multiple tasks with the same Id), and then push this task to completed tasks and then delete this task from the pending task.

But I am not able to achieve it. When I search for a specific task through req.params.tasks, I get an empty array always.

Maybe because taskId is like this : 62863e2ed0d1bc5asas9b04711 whereas it is stored in DB like this ObjectId(62863e2ed0d1bc5asas9b04711)

const specificTasks= PendingTasks.findById(x => x.taskId === req.params.taskId)

How to solve this problem?

Omar Omeiri

ObjectID cannot be compared to a string.

You have to do:

const specificTasks= PendingTasks.findById(x => x.taskId.toString() === req.params.taskId)
// Without mutation
const filt = PendingTasks.filter((elem, i) => i !== PendingTasks.findIndex((t) => t.taskId.toString() === '62863e2ed0d1bc5asas9b04711'));

// With mutation
PendingTasks.splice(PendingTasks.findIndex((t) => t.taskId.toString() === '62863e2ed0d1bc5asas9b04711'), 1);

I ommited the ObjectId because it is not supported in the snippet, but ith should work the same since it is being converted to a string

const PendingTasks = [
  {
    uniqueId: 1212322323,
    taskId: '62863e2ed0d1bc5asas9b04711',
    _id: '6289100e9823422047b7a',
  },
  {
    uniqueId: 1212322323,
    taskId: '62863e2ed0d1bc5asas9b04711',
    _id: '6289100e9823422047b7a',
  },
  {
    uniqueId: 1212322323,
    taskId: '62863e2ed0d1bc5asas9b04341',
    _id: '6289100e9823422047b7a',
  },
];

const filt = PendingTasks.filter((elem, i) => i !== PendingTasks.findIndex((t) => t.taskId.toString() === '62863e2ed0d1bc5asas9b04711'));
console.log('filt: ', filt);

PendingTasks.splice(PendingTasks.findIndex((t) => t.taskId.toString() === '62863e2ed0d1bc5asas9b04711'), 1);
console.log('PendingTasks: ', PendingTasks);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I find a specific String in an array of Objects, searching through each Object in Swift?

'int' object is not subscriptable error - trying to search through 2d array to find specific index

find index of object inside array of objects

jQuery loop through characters in an array of strings and find its index

Get array objects with specific ObjectId in nested array

Find, update an element and change its index in array of objects

Find last element in array from a specific index

Get a specific object from an array of objects

Remove a specific nested object from an array of objects

Select specific object from array of objects jQuery

Access a specific object from an array of objects

Look through an array of objects and remove undefined from its property

Find object by its property in array of objects with AngularJS way

How would you use map to find a specific value from each object in an array of objects

Need to find object from an array with two objects

Find index of objects array

Can you remove an object from an Array by its object condition and not by index?

How to find specific object from array in MongoDB

Iterate through an array of unknown properties to find a specific value in an object

How to retrieve attributes of an object from an array of objects if the type of each object is mongoose.Schema.Types.ObjectId?

Find index of javascript "array of objects" based on object field value

Javascript: Using `.includes` to find if an array of objects contains a specific object

Find index of array with array of objects

How to remove a specific Object from an array of Objects, by object's property?

Search an array of objects to match objectId from mongodb

find the array index of an object with a specific key value in underscore

How delete an object based on its index from a nested array in React?

Sort array of objects and return array of specific values from the object

Convert array of objects to array of specific property from each object

TOP Ranking

HotTag

Archive