【发布时间】:2021-11-20 08:34:47
【问题描述】:
如何使用以下场景将图像存储到 MongoDB。
- 下面是我的产品架构,我想在其中添加另一个字段图像并使用 Product.create 或 Product.insertMany 手动将图像与其他字段一起发布方法(以下示例)。
- 手动插入图片意味着我从 Google 抓取了一些产品图片,但我无法弄清楚如何将它们存储在 MongoDB 中并在我的前端使用它们。
- 我的目标只是使用前端 React 应用显示所有产品以及图片及其详细信息。
const mongoose = require('mongoose');
const productSchema = mongoose.Schema({
name: String,
category: String,
price: Number,
});
const Product = mongoose.model('Product', productSchema);
module.exports = Product;
// Product.insertMany(
// [
// { name: 'Shoe one', category: 'Shoes', price: 3599 },
// { name: 'Shirt one', category: 'Upperwear', price: 380 },
// ],
// (err) => {
// if (err) console.log('error');
// }
// );
【问题讨论】:
-
这能回答你的问题吗? Store images in a MongoDB database
标签: node.js reactjs mongodb mongoose