【发布时间】:2014-04-25 16:59:37
【问题描述】:
在我的 express 应用程序中,当调用下面的 DELETE 方法时,会立即调用 GET 方法,并且它在我的 Angular 代码中给我一个错误,说它应该是一个对象但得到了一个数组。
为什么在我的 DELETE 方法中明确执行 res.send(204); 时会调用我的 GET 方法,我该如何解决这个问题?
服务器控制台:
DELETE /notes/5357ff1d91340db03d000001 204 4ms
GET /notes 200 2ms - 2b
快递备注路线
exports.get = function (db) {
return function (req, res) {
var collection = db.get('notes');
collection.find({}, {}, function (e, docs) {
res.send(docs);
});
};
};
exports.delete = function(db) {
return function(req, res) {
var note_id = req.params.id;
var collection = db.get('notes');
collection.remove(
{ _id: note_id },
function(err, doc) {
// If it failed, return error
if (err) {
res.send("There was a problem deleting that note from the database.");
} else {
console.log('were in delete success');
res.send(204);
}
}
);
}
}
app.js
var note = require('./routes/note.js');
app.get('/notes', note.get(db));
app.post('/notes', note.create(db));
app.put('/notes/:id', note.update(db));
app.delete('/notes/:id', note.delete(db));
angularjs 控制器
$scope.delete = function(note_id) {
var note = noteService.get();
note.$delete({id: note_id});
}
angularjs noteService
angular.module('express_example').factory('noteService',function($resource, SETTINGS) {
return $resource(SETTINGS.base + '/notes/:id', { id: '@id' },
{
//query: { method: 'GET', isArray: true },
//create: { method: 'POST', isArray: true },
update: { method: 'PUT' }
//delete: { method: 'DELETE', isArray: true }
});
});
** 更新 ** 为了帮助绘制图片,这是我得到的角度错误:
Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an object but got an array http://errors.angularjs.org/1.2.16/$resource/badcfg?p0=object&p1=array
我假设我收到此错误是因为我的 delete 方法正在调用我的 get 方法(不知何故),而 get 方法返回整个集合。
【问题讨论】:
-
您可能应该在
exports.get()中检查e,但除此之外,您在此处显示的代码中没有发现任何错误。您确定您的客户端代码没有明确请求 GET 吗? -
这些是出口,它们是如何要求的,它们匹配什么路线?
-
我发布了一些我的 app.js 和我的 angularjs 控制器部分,显示我只是在执行删除而不是另一个 GET。
标签: javascript node.js angularjs express mean-stack