Node.js Mongoose:无法填充ID的嵌套数组

Node.js Mongoose:无法填充ID的嵌套数组,node.js,mongoose,Node.js,Mongoose,我的模式如下所示: const mongoose = require("mongoose"); const Schema = mongoose.Schema; const supplierSchema = new Schema({ name: { type: String, required: true, }, role: { type: String, required: true, }, clients: [

我的模式如下所示:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const supplierSchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  role: {
    type: String,
    required: true,
  },
  clients: [
    {
      _id: false,
      supplierClientID: {
        type: mongoose.Types.ObjectId,
        ref: "Customer",
      },
      orders: [{ type: mongoose.Types.ObjectId, ref: "Order" }],
    },
  ],
});

module.exports = mongoose.model("Supplier", supplierSchema);
我想填充orders数组,它是数据库中属于order的order ID数组。但是,我在尝试执行此操作时遇到以下错误:

CastError: Cast to ObjectId failed for value "" at path "_id" for model "Order"
    at model.Query.exec (C:\teamSIO\server\node_modules\mongoose\lib\query.js:4351:21)
    at model.Query.Query.then (C:\teamSIO\server\node_modules\mongoose\lib\query.js:4443:15)
    at exports.getSupplierClientsDetails (C:\teamSIO\server\controllers\supplierFlow.js:43:6)
    at Layer.handle [as handle_request] (C:\teamSIO\server\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\teamSIO\server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\teamSIO\server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\teamSIO\server\node_modules\express\lib\router\layer.js:95:5)
    at C:\teamSIO\server\node_modules\express\lib\router\index.js:281:22
    at param (C:\teamSIO\server\node_modules\express\lib\router\index.js:354:14)
    at param (C:\teamSIO\server\node_modules\express\lib\router\index.js:365:14)
    at param (C:\teamSIO\server\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (C:\teamSIO\server\node_modules\express\lib\router\index.js:410:3)
    at next (C:\teamSIO\server\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\teamSIO\server\node_modules\express\lib\router\index.js:174:3)
    at router (C:\teamSIO\server\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\teamSIO\server\node_modules\express\lib\router\layer.js:95:5) {
  messageFormat: undefined,
  stringValue: '""',
  kind: 'ObjectId',
  value: '',
  path: '_id',
  reason: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
      at new ObjectID (C:\teamSIO\server\node_modules\bson\lib\bson\objectid.js:59:11)
      at castObjectId (C:\teamSIO\server\node_modules\mongoose\lib\cast\objectid.js:25:12)
      at ObjectId.cast (C:\teamSIO\server\node_modules\mongoose\lib\schema\objectid.js:267:12)
      at ObjectId.SchemaType.applySetters (C:\teamSIO\server\node_modules\mongoose\lib\schematype.js:1031:12)
      at ObjectId.SchemaType._castForQuery (C:\teamSIO\server\node_modules\mongoose\lib\schematype.js:1459:15)
      at ObjectId.SchemaType.castForQuery (C:\teamSIO\server\node_modules\mongoose\lib\schematype.js:1449:15)
      at C:\teamSIO\server\node_modules\mongoose\lib\schematype.js:1398:18
      at Array.ma
填充订单数组的代码如下所示:

const { currentSupplierID, supplierClientID } = req.params;
  Supplier.findOne(
    {
      _id: currentSupplierID,
      "clients.supplierClientID": supplierClientID,
    },
    { clients: 1 }
  )
    .populate("clients.supplierClientID", "logo name city country")
    .populate("clients.orders")
    .then((result) => {
      if (result.clients) {
        res.status(200).json(result);
      } else {
        return res
          .status(404)
          .json({ NotFoundError: "Requested Supplier Does Not Exist" });
      }
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json(err);
    });
它能够很好地填充supplierClientID对象,但却无法填充orders数组

我试图提取的数据的屏幕截图如下所示:

我必须将模式中的orders数组更改为:

clients: [
    {
      _id: false,
      supplierClientID: {
        type: mongoose.Types.ObjectId,
        ref: "Customer",
      },
      orders: [ { orderID: { type: mongoose.Types.ObjectId, ref: "Order" } }],
    },
  ],
通过这种方式,我能够在引用订单时正确地进行填充