我的架构如下:
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的数据库中的订单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对象,但又无法填充订单数组。
答案 0 :(得分:0)
我必须将模式中的orders数组更改为此:
clients: [
{
_id: false,
supplierClientID: {
type: mongoose.Types.ObjectId,
ref: "Customer",
},
orders: [ { orderID: { type: mongoose.Types.ObjectId, ref: "Order" } }],
},
],
这样,我在引用订单时就能正确地进行填充。