【问题标题】:How do I add a req.body to a mongoose array?如何将 req.body 添加到 mongoose 数组?
【发布时间】: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'
}

【问题讨论】:

    标签: arrays mongoose


    【解决方案1】:

    Array#push 在将元素压入后返回数组的新长度。

    const galArray = img => {
        var gal = [];
        gal.push(img); // You should push the element into the array
        return gal; // And then return the array
    }
    

    【讨论】:

    • 我刚试过,还是不行。这个对吗? var image = {'image': req.body.image};
    • 你能显示req.body.image的输出吗?
    猜你喜欢
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2016-03-17
    • 2012-09-04
    • 2019-01-04
    • 2019-02-13
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多