【发布时间】:2020-03-27 22:13:38
【问题描述】:
我觉得我几乎什么都试过了。
我想将 .ejs 文件中的图像添加到图库数组中。 req.body 有效,我只是无法将这些图像添加到画廊数组中。
router.post("/", function(req, res){
var name = req.body.name;
var mainImage = req.body.mainImage;
var image = {'image': req.body.image}; <----------
var gallery = galArray(image); <----------
var description = req.body.description;
var price = req.body.price;
var newBike = {name: name, mainImage: mainImage, gallery: gallery, description: description, price: price}
Bike.create(newBike, function(err, newlyCreated){
if(err){
console.log(err);
} else {
console.log(newlyCreated);
res.redirect("/shop/bikes");
}
});
});
const galArray = img => {
var gal = []; <-----------------------
return gal.push(img);
}
const bikeSchema = new mongoose.Schema({
// IDENTITY
name: String,
mainImage: String,
gallery: [{
image: String. <----------
}
],
description: String,
我试过了:
var image = req.body.image;
var gallery = gallery.push(image);
var image = {'image': req.body.image};
var gallery = gallery.push(image);
编辑: 这是我将函数更改为后收到的错误消息:
const galArray = img => {
var gal = [];
gal.push(img);
return gal;
}
这是错误信息:
stringValue: '"[\n' +
" '/public/img/Bikes/Road/superstar bottom bracket.jpeg',\n" +
" '/public/img/Bikes/Road/superstar bottom bracket.jpeg',\n" +
" '/public/img/Bikes/Road/superstar bottom bracket.jpeg',\n" +
" '/public/img/Bikes/Road/superstar bottom bracket.jpeg',\n" +
" '',\n" +
" '',\n" +
" '',\n" +
" ''\n" +
']"',
kind: 'String',
value: [Array],
path: 'image',
reason: [MongooseError],
message: 'Cast to String failed for value "[\n' +
" '/public/img/Bikes/Road/superstar bottom bracket.jpeg',\n" +
" '/public/img/Bikes/Road/superstar bottom bracket.jpeg',\n" +
" '/public/img/Bikes/Road/superstar bottom bracket.jpeg',\n" +
" '/public/img/Bikes/Road/superstar bottom bracket.jpeg',\n" +
" '',\n" +
" '',\n" +
" '',\n" +
" ''\n" +
']" at path "image"',
name: 'CastError'
}
},
_message: 'Bike validation failed',
name: 'ValidationError'
}
【问题讨论】: