I have a schema like this:
const Person = new mongoose.Schema({
username: {
type: String,
required: [true, "Name is a required field"],
unique: [true, "Another Person with the same name already exists"],
trim: true
},
friends: [
{
name: {
type: String,
required: [true, "Every friend must have a name"],
unique: [true, "Another friend with the same name already exists"],
trim: true
},
photo: String,
favoriteFood: String
}
],
createdAt: {
type: Date,
default: Date.now()
}
});
I want to add an image for each friend, but I want to upload the photo from something like postman or a seeder file by adding image address in my hard drive in the request. because I have several friends, I have to make each photo specific for each friend. I want to do something like this in postman body or in a file as a seeder to fill database or add data to it.
{
"username": "aUsername",
"friends": [
{
"name": "friend1",
"photo": "./home/user/photos/photo1.jpg",
"favoriteFood": "Pizza"
},
{
"name": "friend2",
"photo": "./home/user/photos/photo2.jpg",
"favoriteFood": "Sushi"
},
{
"name": "friend3",
"photo": "./home/user/photos/photo3.jpg",
"favoriteFood": "Hamburger"
}
]
}
how can I create this feature in the backend?
thanks.
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, '/src/my-images');
},
filename: function (req, file, callback) {
callback(null, file.fieldname);
}
});
app.post('/', upload.single('file'), (req, res) => {
if (!req.file) {
console.log("No file received");
return res.send({
success: false
});
} else {
console.log('file received');
return res.send({
success: true
})
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.