MEANjs mongoose中间件+ ngtable分页 - 所有页面都从寄存器0开始(分页不起作用)

时间:2015-03-19 19:23:05

标签: angularjs pagination mongoose meanjs

我正在使用mongoose-middleware http://bazalt-cms.com/ng-table/example/1跟踪此处描述的ng-table示例:https://github.com/PlayNetwork/mongoose-middleware。我的代码如下所示。 列表insumos.client.view.html:

<section data-ng-controller="InsumosController" data-ng-init="find()">
    <div class="page-header">
        <h1>Insumos</h1>
    </div>      
    <table ng-table="tableParams">
        <tr ng-repeat="insumo in $data">
            <td data-title="'Nombre'"> {{insumo.name}}</td>
....

insumos.server.controller.js:

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    errorHandler = require('./errors.server.controller'),
    Insumo = mongoose.model('Insumo'),
    _ = require('lodash');

 .....
/**
 * List of Insumos
 */
exports.list = function(req , res){

    var count = req.query.count || 5;
    var page = req.query || 1   ; 

    var options = {
        filters : {
            mandatory : { contains : req.query.filter       }
        },
        sort : {
            asc : '_id',                
        },
        start: (page-1)*count,
        count: count
    };

    Insumo
        .find()
        .keyword(options)
        .filter(options)
        .order(options)
        .page(options,   function (err, insumos) {
                if (!err) {
                    //console.log(insumos.results); //this gives me the array I was looking for
                   ** res.jsonp(insumos); ** // the good stuff in insumos.results but I need to pass this in order to make the client controller take it as an object and use it for pagination in insumos.client.controller
                } else {    console.log(err);     }
            });
};
....

insumos.client.controller.js:

'use strict';

// Insumos controller
angular.module('insumos').controller('InsumosController',   ['$scope', '$stateParams', '$location', 'Authentication', 'Insumos', **'ngTableParams' ** , '$filter',  function($scope, $stateParams, $location, Authentication, Insumos, **ngTableParams**, $filter) {
        $scope.authentication = Authentication;

        var params = {
            page: 1,            // show first page
            count: 5,          // count per page
        };
        var settings = {
            total: 0,           // length of data
            getData: function($defer, params) {
                Insumos.get(params.url(), function(response){
                    params.total(response.total);
                    $defer.resolve(response.results); //creates an obj $data with the  api results ("results" type is object, althought it should be Array... it is an object that contains 5 arrays)
                });
            }};

        $scope.tableParams = new **ngTableParams**(params, settings); //I get a warning here saying I need to make this uppercase, but I think it's cool to leave it like that
.....

现在......我的名单上有5个项目(这很好)。当我单击表格操作按钮显示10个项目时,它完美地工作,它显示前十个项目(总现有项目:13)。到目前为止都很好。我点击“下一页”,没有任何反应。我检查控制台,它说:

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object
http://errors.angularjs.org/1.2.28/$resource/badcfg?p0=array&p1=object

所以我 console.log(typeof(response.results)+' - --- - '+ response.results)我得到了

object - --- - [object Object],[object Object],[object Object],[object Object],[object Object]

所以...我有点被困在这里......我应该为做并将每个结果推送到$ defer.resolve吗?我尝试了所有关于将对象转换为数组并且没有改变任何东西的技巧 有谁知道应该收到什么?


仍然......列表已填满......前五项。我查看了“网络”标签,看了每次点击“下一页”按钮时发生了什么 并且似乎所有页面都具有相同的前5项...... 相同前5项。无论我在哪个页面,它总是从第0项开始。

    options: {filters: {mandatory: {}}, sort: {asc: "_id"}, start: 0, count: "5"}
    count: "5"
    filters: {mandatory: {}}
    sort: {asc: "_id"}
    start: 0
    results: [{_id: "550763b325a55de01172d182", user: "5507633625a55de01172d180", __v: 0,…},…]
    0: {_id: "550763b325a55de01172d182", user: "5507633625a55de01172d180", __v: 0,…}
    1: {_id: "550763ba25a55de01172d183", user: "5507633625a55de01172d180", __v: 0,…}
    2: {_id: "550832c26a3755ec289ecabf", user: "5507633625a55de01172d180", __v: 0,…}
    3: {_id: "55087480332393bb3bb11ca3", user: "5507633625a55de01172d180", __v: 0,…}
    4: {_id: "550878d9a3b14ece3d112b6d", user: "5507633625a55de01172d180", __v: 0,…}
    total: 13

无论我设定哪个或哪个页面,它始终从第0项开始

insumos?count=10&page=1  - GET  304 Not Modified
insumos?count=10&page=2  - GET  304 Not Modified
insumos?count=5&page=1    - GET 304 Not Modified
insumos?count=5&page=2    - GET 304 Not Modified
insumos?count=5&page=3    - GET 304 Not Modified

所以问题是:

1-为什么分页不改变列表中的项目?我怎样才能使它工作? 2-为什么“预期和数组但有一个对象”错误?我怎么能摆脱它?

百万感谢提前阅读并花时间回复的人

2 个答案:

答案 0 :(得分:0)

由于慷慨的朋友,我发现我的鼻子前面总是有一个错误: 它没有改变页面,因为在文件insumos.server.controller.js中我有这个代码

exports.list = function(req , res){
  var count = req.query.count || 5;
  var page = req.query || 1   ; 

应该是

exports.list = function(req , res){
  var count = req.query.count || 5;
  var page = req.query**.page** || 1   ; 

谢谢,Dwira Maulana!

我一直收到控制台错误     错误:[$ resource:badcfg]资源配置错误。预期的响应包含一个数组但得到一个对象     http://errors.angularjs.org/1.2.28/ $资源/ badcfg P0 =阵列&安培; P1 =对象

----但我的主要关注点已经解决了......如果有人想要解决这个问题,我会非常感激

答案 1 :(得分:0)

要修复该错误,只需将角度资源返回操作更改为

即可
'get': {method: 'GET', isArray: false}

,覆盖默认值。 此外,您应该从方法get更改为query,因为get按定义用于获取一条记录,而不是列表。

'query': {method: 'GET', isArray: false}