下面的表单使用 handlelebars (.hbs) 引擎和 multer-gridfs-storage (Mongodb FIles) 将图像/文件发送到 Mongodb,但单击删除会产生以下错误:
类型错误:gfs.deleteOne 不是函数
此外,它不会加载所附照片的缩略图
Ps:我认为错误是HBS形式。
完整代码: https ://github.com/rebek/upFileHBS
<form action="/upload" method="POST" enctype="multipart/form-data">
<div class="custom-file mb-3">
<input type="file" name="file" id="file" class="custom-file-input">
<label for="file" class="custom-file-label">Choose File</label>
</div>
<input type="submit" value="Submit" class="btn btn-primary btn-block">
</form>
<hr>
{{#each files}}
<div class="card card-body mb-3">
{{#if file.isImage}}
<img src="image/{{this.filename}}" alt="">
{{else}}
{{this.filename}}
<form method="POST" action="/files/{{this._id}}?_method=DELETE">
<button class="btn btn-danger btn-block mt-4">Delete</button>
</form>
</div>
{{/if}}
{{/each}}
</div>
</div>
</div>
app.get('/', (req, res) => {
gfs.files.find().toArray((err, files) => {
// Check if files
if (!files || files.length === 0) {
res.render('index', { files: false });
} else {
files.map(file => {
if (
file.contentType === 'image/jpeg' ||
file.contentType === 'image/png'
) {
file.isImage = true;
} else {
file.isImage = false;
}
});
res.render('index', { files: files });
}
});
});
...
app.delete('/files/:id', (req, res) => {
gfs.deleteOne({ _id: req.params.id, root: 'uploads' }, (err, GridFSBucket) => {
if (err) {
return res.status(404).json({ err: err });
}
res.redirect('/');
});
});