【问题标题】:ExpressJS why is my GET method called after my DELETE method?ExpressJS 为什么在我的 DELETE 方法之后调用我的 GET 方法?
【发布时间】: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


【解决方案1】:

服务器端

您正在从 delete 函数的集合中删除一个元素。这是异步完成的,并在完成时调用您的回调。

在此期间,执行了其他请求,这就是为什么您的GET 请求在您的DELETE 请求完成之前执行的原因。

同样的情况发生在您的 get 函数中,您正试图从集合中查找元素,而此函数过于异步。

但这只是服务器端,没关系,它应该这样工作,你的问题位于客户端。

客户端

如果你想删除你的笔记之后你得到它,你必须在你的角度控制器中使用一个回调函数,它只会在你得到你的笔记时被调用(如果你需要帮助那,向我们展示你的noteService 角码)。

这是一些基本的 javascript 理解问题,动作通常是异步进行的,你需要回调才能有一个执行链。

也许尝试做这样的事情:

$scope.delete = function(note_id) {
    var note = noteService.get({ id: note_id }, function()
    {
        note.$delete();
    });
}

您的代码没有意义,为什么$scope.delete 中有get?为什么不简单如下:

$scope.delete = function(note_id) {
    noteService.delete({ id: note_id });
}

错误

我认为您收到此错误是因为您的服务器在您的 exports.delete 函数中发送的内容。当 angular 需要一个对象(REST API 从不发送字符串)时,您正在发送字符串或根本不发送内容。您应该发送类似的内容:

res.send({
    results: [],
    errors: [
        "Your error"
    ]
});

【讨论】:

  • 我认为你离题了(不想听起来刺耳)。我加载调用我的 GET 方法的页面。然后我单击一个删除按钮,该按钮调用我的角度 $scope.delete 方法。此时,不应调用任何 GET 方法,因为在我的角度代码或我的 express DELETE 方法中都没有调用它。
  • 这是什么意思:This is done asynchronously and calling your callback when it's finished。没有定义回调。
  • @Catfish 这就是问题所在,你应该定义一个,看我编辑的答案。
  • 你有一个有效的观点:noteService.delete({ id: note_id });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 2014-12-14
  • 2013-04-17
  • 1970-01-01
相关资源
最近更新 更多