CastError: Cast to ObjectId failed for value "cover.css"
CastError: Cast to ObjectId failed for value "cover.css"
我尝试使用 NodeJS 和 express 在我的 mongoDB 中保存新数据。 Cover.css
来自 Bootstrap。
我通过在我的架构中添加 _Id
找到了解决方案,但我需要让 mongo 成为 _Ids
。我无法理解这个错误。通常,如果我没有在我的模式中提及 _Id
,mongoose 将创建一个。但是在这个错误中,似乎我必须手动发送一个 Id。我不知道为什么
在这里
CastError: Cast to ObjectId failed for value "cover.css" at path "_id" for model "ProductModel"
messageFormat: undefined,
stringValue: '"cover.css"',
kind: 'ObjectId',
value: 'cover.css',
path: '_id',
reason: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
还有我的代码:
const express = require('express')
var ProductModel = require('./../models/article')
const router = express.Router()
router.get('/new', (req, res) => {
res.render('products/new', { product: new ProductModel() })
})
router.get('/:id/', async (req, res) => {
var product = await ProductModel.findById(req.params.id)
res.render('products/show', { article: product })
router.post('/', async(req, res) => {
let product = new ProductModel({
Type: req.body.Type,
Boitier: req.body.Boitier,
Marque: req.body.Marque,
Modele: req.body.Modele,
Annee: req.body.Annee,
Poids: req.body.Poids,
Capteur: req.body.Capteur,
Resolution: req.body.Resolution,
ISO: { min: req.body.min, max: req.body.max },
Dimensions: { Hauteur: req.body.Hauteur, Longueur: req.body.Longueur, Profondeur: req.body.Profondeur },
Charge: req.body.Charge,
Monture: req.body.Monture,
Connectique: { USB: req.body.USB, HDMI: req.body.HDMI, mic: req.body.mic, priseCourant: req.body.priseCourant },
Puissance: req.body.Puissance,
Temperature: req.body.Temperature,
Lampe: req.body.Lampe,
Focale: req.body.Focale
})
try {
product = await product.save()
res.redirect(`/products/${product.id}/`)
} catch (e) {
res.render('/products/new', { article: product })
res.send('HUM... embarrassant')
console.log(e)
}
})
的。解决方案就是在模板 link 中使用 href="/cover.css"
。问题现已解决
我尝试使用 NodeJS 和 express 在我的 mongoDB 中保存新数据。 Cover.css
来自 Bootstrap。
我通过在我的架构中添加 _Id
找到了解决方案,但我需要让 mongo 成为 _Ids
。我无法理解这个错误。通常,如果我没有在我的模式中提及 _Id
,mongoose 将创建一个。但是在这个错误中,似乎我必须手动发送一个 Id。我不知道为什么
在这里
CastError: Cast to ObjectId failed for value "cover.css" at path "_id" for model "ProductModel"
messageFormat: undefined,
stringValue: '"cover.css"',
kind: 'ObjectId',
value: 'cover.css',
path: '_id',
reason: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
还有我的代码:
const express = require('express')
var ProductModel = require('./../models/article')
const router = express.Router()
router.get('/new', (req, res) => {
res.render('products/new', { product: new ProductModel() })
})
router.get('/:id/', async (req, res) => {
var product = await ProductModel.findById(req.params.id)
res.render('products/show', { article: product })
router.post('/', async(req, res) => {
let product = new ProductModel({
Type: req.body.Type,
Boitier: req.body.Boitier,
Marque: req.body.Marque,
Modele: req.body.Modele,
Annee: req.body.Annee,
Poids: req.body.Poids,
Capteur: req.body.Capteur,
Resolution: req.body.Resolution,
ISO: { min: req.body.min, max: req.body.max },
Dimensions: { Hauteur: req.body.Hauteur, Longueur: req.body.Longueur, Profondeur: req.body.Profondeur },
Charge: req.body.Charge,
Monture: req.body.Monture,
Connectique: { USB: req.body.USB, HDMI: req.body.HDMI, mic: req.body.mic, priseCourant: req.body.priseCourant },
Puissance: req.body.Puissance,
Temperature: req.body.Temperature,
Lampe: req.body.Lampe,
Focale: req.body.Focale
})
try {
product = await product.save()
res.redirect(`/products/${product.id}/`)
} catch (e) {
res.render('/products/new', { article: product })
res.send('HUM... embarrassant')
console.log(e)
}
})
的。解决方案就是在模板 link 中使用 href="/cover.css"
。问题现已解决