Utilizing the schemas below to represent product specifications,
const MoldSpecific = new Schema(
{
name: {
en: String,
ar: String,
kr: String,
},
values: [MoldValue.schema],
},
{
versionKey: false,
}
);
export default model("MoldSpecific", MoldSpecific);
const MoldValue = new Schema(
{
name: {
en: String,
ar: String,
kr: String,
},
unit: { type: String, default: "" },
},
{
versionKey: false,
}
);
export default model("MoldValue", MoldValue);
Users can select various specifics such as color or manufacture year, size, etc. To handle query parameters and construct the query, the code snippet below is used. However, encountering issues while mapping or joining elements in the array:
export function getQueryParams(query) {
var queryParams = {};
var specsParams = [];
if (query.year) {
specsParams.push({ $elemMatch: { "values._id": query.year } });
}
if (query.color) {
specsParams.push({ $elemMatch: { "values._id": query.color } });
}
queryParams["specifics"] = {
$all: [specsParams.join()],
};
console.log(queryParams);
return queryParams;
}
Despite using both map and join functions with specParams array, it results in an error:
ObjectParameterError: Parameter \"obj\" to Document() must be an object, got [object Object],[object Object]"
Note: The following code snippet works successfully:
queryParams["specifics"] = {
$all: [specsParams[0]],
};
Any assistance or insights would be greatly appreciated.