I try to save a new data in my mongoDB with NodeJS and express. Cover.css
comes from Bootstrap.
I found a solution by adding _Id
in my Schema, but I need to let mongo make _Ids
. I can't understand this error. Normally , if I doesnt mention an _Id
on my schema, mongoose will create one. But in this error, it seems like I have to send an Id manually. I don't know why
Here is it
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
And my code :
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)
}
})
ok. the solution is just to use href="/cover.css"
in template link. question is now resolved