【发布时间】:2020-11-08 04:07:09
【问题描述】:
但不知道如何解决我的问题,因为我认为我传递了正确的值。
我的类别架构:
const mongoose = require("mongoose");
const categorySchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: true,
maxlength: 32,
unique: true,
},
},
{ timestamps: true }
);
module.exports = mongoose.model("Category", categorySchema);
我引用类别的产品架构:
const { ObjectId } = mongoose.Schema;
category: [
{
type: ObjectId,
ref: "Category",
required: true,
},
],
我得到的错误:
Error: Product validation failed: category: Cast to [ObjectId] failed for value "["5f09bc7d75350639906e0822,5f0df6442400aa0344d64347"]" at path "category"
stringValue: '"["5f09bc7d75350639906e0822,5f0df6442400aa0344d64347"]"',
messageFormat: undefined,
kind: '[ObjectId]',
value: '["5f09bc7d75350639906e0822,5f0df6442400aa0344d64347"]',
path: 'category',
reason: [CastError] } },
我如何制作产品:
form.parse(req, (err, fields, file) => {
if (err) {
return res.status(400).json({
error: "Problem with image",
});
}
//destructure the fields
const { name, description, price, category, stock } = fields;
if (!name || !description || !price || !category || !stock) {
return res.status(400).json({
error: "All fields are required!",
});
}
//TODO : rescrition on fields
let product = new Product(fields);
【问题讨论】:
标签: node.js reactjs mongodb mongoose