返回资源对象而不是 Mongoose.count 值

Resource object returned instead of Mongoose.count value

我在 mean.js 应用程序中尝试从后端 API 的函数中检索 Mongoose.count 值时遇到了一些问题。据我所知,一切都已正确路由,并且在我到达前端 angular 代码并尝试通过服务和 $resource 从 API 检索数据之前似乎工作得非常好.

这是我的后端 API 函数,用于检索特定类别中的列表计数,其中类别作为参数正确传递。

exports.getListingCountForSpecificCategory = function(req, res, next, category) {
    Listing.count({
        category: category,
        quantityAvailable: { $gt: 0 },
        listingActive: true
    }, function(err, count){
        if(err) {
            console.log(err);
        } else {
            console.log(category + ': ' + count);
            res.jsonp(count);
        }
    });
}; 

这 运行 正确并且在 console.log()

console.log(category + ': ' + count);

正确返回类别计数。所以,一切正常!

直到我在前端使用 angular 检索计数值。这是我写的函数:

$scope.listingsCount = function(category) {
    $scope.catCount = Listings.listingsCategoryCount.query({
        category: category.alias
    });

    $scope.catCount.$promise.then(function(count){
        category.listingCount = count;
    });
};

当此函数为 运行 并且将类别对象传递给它时,而不是检索计数值,例如14,似乎取而代之的是检索一个资源承诺对象,计数值无处可见。我查看了代码几次,但终究无法弄清楚原因。

这是我正在使用的服务,以备不时之需。

listingsCategoryCount: $resource('listingscount/:category', {
    category: '@_category'
}, {
    query: {
        method: 'GET',
        isArray: false
    }
}),

为什么没有返回计数值对我来说是个谜。我当然可能会错误地解决这个问题。任何帮助将不胜感激。

谢谢

https://docs.angularjs.org/api/ngResource/service/$资源:

On success, the promise is resolved with the same resource instance or collection object, updated with data from server.

将此更改为:res.jsonp(count);res.jsonp({ count: count }); 它应该可以工作:您将得到一个带有 属性 count.[=16 的 Resource 对象=]