【问题标题】:MEANjs mongoose middleware + ngtable Pagination - all pages start at register 0 (pagination doesn't work)MEANjs mongoose 中间件 + ngtable 分页 - 所有页面都从寄存器 0 开始(分页不起作用)
【发布时间】:2015-05-23 01:53:23
【问题描述】:

我正在关注此处描述的 ng-table 示例:http://bazalt-cms.com/ng-table/example/1 using mongoose-middleware 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]

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


仍然......列表被填满......前五个项目。我检查了“网络”选项卡,并查看了每次单击“下一页”按钮时发生的情况 似乎所有页面都有相同的前 5 个项目。same 前 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-为什么是“预期和数组但有一个对象”错误?我怎样才能摆脱它?

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

【问题讨论】:

    标签: angularjs pagination mongoose meanjs


    【解决方案1】:

    感谢一位慷慨的朋友,我发现一直有一个错误摆在我的面前: 它没有改变页面,因为在 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   ; 
    

    谢谢,德维拉毛拉纳!

    我不断收到控制台错误 错误:[$resource:badcfg] 资源配置错误。包含数组但得到对象的预期响应 http://errors.angularjs.org/1.2.28/$resource/badcfg?p0=array&p1=object

    ----但是我的主要问题已经解决了....如果有人想帮助解决这个问题,我将非常感谢

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-16
        • 1970-01-01
        • 2012-10-29
        • 2013-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多