我有一个LivingArea对象的mongodb数据库。我想使用其ID从数据库中删除一个对象:
'use strict';
exports = module.exports = ( LivingAreaModel ) => {
return function* (id) {
let h = this.request.header,
b = this.request.body,
rec = yield LivingAreaModel.findById( {
_id: id
} ).remove().exec();
this.success( {
livingAreas: rec
} );
};
};
exports[ '@singleton' ] = true;
exports[ '@require' ] = [
'model/livingAreaModel'
];
我使用以下路线:
{
"method": "delete",
"path": "/livingarea/delete/:id",
"handler": "handlers/livingarea/remove.js",
"validate": [ {
"param": "id",
"type": "body",
"validators": [
"notEmpty",
"isString"
]
}]
}
但是当我这样打电话时:http://localhost:8000/api/v1/livingarea/delete/5783c4da66ac31c814911400 使用db中一个对象的id,我收到错误:
{"status":"error","error":{"code":400,"msg":"id is empty"}}
哪里可能是问题?
提前致谢!
答案 0 :(得分:0)
我终于找到了答案。方法findById
需要id,而不是对象。更多,路线必须看起来像这样:
{
"method": "delete",
"path": "/livingarea/delete/:id",
"handler": "handlers/livingarea/remove.js"
}