jatinder bhola jatinder bhola - 3 years ago 183
JSON Question

Can't read MongoDB Id format | displaying as bufferarray

I am learner and student. I was trying to learn nodejs + mongoose. I was performing a comparison operation on mongoose id with two different collections.

Model design
Model one:

var ShowroomSchema = new Schema({ //ShowroomProduct/Item
rug: { //product/itemDetails
...
colourTags: [Schema.Types.ObjectId],
...
}
});


model two:

var ShowroomColourTagSchema = new Schema({
name: String;
})


I am storing the id's of ShowroomColourTagSchema in my ShowroomSchema and later trying to perform a loop to check with id's are equals so I will print the name of the color.

function

//colour tags
for (var index = 0; index < models.length; index++) { // model two id, name
var element = models[index];

for (var j = 0; j < item.rug.colourTags.length; j++) { // model one just array of object ids
// for (var j in item.rug.sizeCategoryTags) {
if (element._id.equals(item.rug.colourTags[j]._id)) {
tags.push(element.name);
}
}
}


but I am facing issues in trying to make the condition work. I am trying to debug the code (from last 4-5hours). The watch expression is showing the colourTags from showroom as buffer array.

enter image description here

Backend

showroom_colour_tags collection

{
"_id" : ObjectId("59c4310ada77350004cef56c"),
"updated_at" : ISODate("2017-09-21T21:37:14.043+0000"),
"created_at" : ISODate("2017-09-21T21:37:14.043+0000"),
"name" : "Black",
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("59c43114da77350004cef56e"),
"updated_at" : ISODate("2017-09-21T21:37:19.378+0000"),
"created_at" : ISODate("2017-09-21T21:37:19.378+0000"),
"name" : "Grey",
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("59c43170da77350004cef574"),
"updated_at" : ISODate("2017-09-21T21:37:24.036+0000"),
"created_at" : ISODate("2017-09-21T21:37:24.036+0000"),
"name" : "Beige",
"__v" : NumberInt(0)
}


showroom collection

{
"rug" : {
...
"colourTags" : [
ObjectId("59c43170da77350004cef574"),
ObjectId("59c43114da77350004cef56e")
]
...
}
}


How can I compare the ID's????! I will be very thankful for any suggestion. Thanks in advance. Let me know if you require more information.
Peace :)

Answer Source

remove _id from item.rug.colourTags[j]._id , colourTags is an array so you need to compare with position. your updated if condition will be.

if (element._id.equals(item.rug.colourTags[j])) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download