Wednesday, 10 November 2021

Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters while using OR condition in node js / javascript

I want to fetch data from database by _id and ownerId so I gave OR condition

but getting this error Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters

my code as follow

var ObjectId = require("mongodb").ObjectId;


const getDetailById = async (req, res) => {
  const { id } = req.params;
  try {
    let detail = await Task.find(
      { $or: [{ ownerId: id }, { _id: ObjectId(id) }] }
    ).populate("shop");
    } catch (err) {
    sendError(401, "Cannot get detail by given id", err.message, req, res);
  }
};

here I can get data from database by passing _id which is 24 character but when I tried to fetch data by ownerId its shows me this error

Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters

my output look like this

 {
      "_id": "61483aa5e6dcd5bb6b2c9c58",
      "ownerId": "b0jytaonktsc5ghf",
      "ownerName": "check demo",
      "ownerDescription": "Testing purpose",
      "shop": {
        "_id": "61483aa5e6dcd5bb6b2c9c55",
        "shopName": "Aakash",
        "shopPlace": "Mumbai",
        "__v": 0
      },
      
      "createdAt": "2021-09-20T07:39:17.528Z",
      "updatedAt": "2021-09-20T07:39:17.528Z",
      "__v": 0
    },

and This is my Schema

const mongoose = require("mongoose");
const uniqid = require("uniqid");

const ownerSchema = new mongoose.Schema(
  {
    ownerId: {
      type: String,
      default: uniqid(),
      unique: true,
    },
    OwnerName: {
      type: String,           
    },
    taskTag: [
      {
        type: String,
      },
    ],
    onwerDescription: {
      type: String,
    },       
    shop: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "peoples",
      default: null,
    },    
  },
  { timestamps: true }
);

const owner = mongoose.model("task", ownerSchema);

module.exports = owner;



from Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters while using OR condition in node js / javascript

No comments:

Post a Comment