JuJ*_*oDi 5 mongoose node.js npm express
我有一个照片模型:
var photoSchema = mongoose.Schema({
name: { type: String},
path: { type: String},
vehicle: { type: ObjectId, ref: 'Vehicle' }
});
Run Code Online (Sandbox Code Playgroud)
使用该路线删除照片:
app.delete('/api/photos/:id/delete', photos.delete (app.get('photos')));
Run Code Online (Sandbox Code Playgroud)
我可以使用以下命令删除mongo中的照片:
exports.deletePhoto = function (dir) {
function (req, res, next) {
var id = req.params.id;
Photos.delete (id, function (err) {
if (err) return next (err);
return res.send ({
status: "200",
responseType: "string",
response: "success"
});
});
};
};
Run Code Online (Sandbox Code Playgroud)
照片保存在磁盘上
app.set('photos', __dirname + '/public/photos');
Run Code Online (Sandbox Code Playgroud)
如何从deletePhoto功能中删除磁盘中的照片?
您可以执行以下操作:
路由器
app.delete('/api/photos/:id', photos.deletePhoto);
Run Code Online (Sandbox Code Playgroud)
它遵循REST api规范
出口
exports.deletePhoto = function (req, res) {
Photos.remove({_id: req.params.id}, function(err, photo) {
if(err) {
return res.send({status: "200", response: "fail"});
}
fs.unlink(photo.path, function() {
res.send ({
status: "200",
responseType: "string",
response: "success"
});
});
});
};
Run Code Online (Sandbox Code Playgroud)