【问题标题】:Angularjs http get request return 'length' errorAngularjs http获取请求返回“长度”错误
【发布时间】:2017-09-24 15:52:06
【问题描述】:

我是 Angularjs 和 MongoDB 的新手,当我发送到 git hub API 时,我需要从 MongoDB 发送 HTTP 请求以获取自动完成的角度材料,它运行良好,但对于我的数据库,我得到“无法读取未定义的属性“长度” ' 我在客户端的控制器

    /*jslint latedef:false*/
(function () {
  'use strict';
  angular
      .module('clientApp')
      .controller('CarnetCtrl', function($http){

        this.querySearch = function(query){
        //for this link work "https://api.github.com/search/users"  
       return$http.get("http://localhost:3000/carnet/export_countries/", {
                params: {
                    q: query
                }
            }).then(function(response){
                return response.data.items;
            });

        };
      });

})();

我的 Mongodb 控制器

    var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Q = require('q'); // We can now use promises!



//Create Schema
var CarnetExportCountriesSchema = new Schema({


CarnetExportCountriesSchema.statics.getCarnetExportCountries = function(Country_Name_EN) {

    var deferred = Q.defer();

    this.findOne({Country_Name_EN: Country_Name_EN}, function(error, CarnetExportCountries){

        if (error) {

            deferred.reject(new Error(error));
        }
        else {

            deferred.resolve(manufacturers);
        }
    });

    return deferred.promise;
}


CarnetExportCountriesSchema);



module.exports = CarnetExportCountriesSchema;

和 index.js 服务器端:

//Add Middleware necessary for REST API's
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(bodyParser.json({type:'application/vnd.api+json'}));
app.use(methodOverride('X-HTTP-Method-Override'));

//CROS Support
app.use(function(req,res,next){
    res.header('Access-Control-Allow-Origin','*');
    res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers','Content-Type');
    res.header('("Content-Length", ""+json.length() );');
    next();
});

我的 app.js

  .config(function (
    $routeProvider,
    RestangularProvider
    ) {
    RestangularProvider.setBaseUrl('http://localhost:3000');
    $routeProvider
      .when('/main', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl',
        controllerAs: 'main'
      })

我的错误:

TypeError: Cannot read property 'length' of undefined
    at hasMatches (angular-material.js:25423)
    at shouldShow (angular-material.js:25415)
    at shouldHide (angular-material.js:25380)
    at setLoading (angular-material.js:25371)
    at angular-material.js:25564
    at handleCallback (angular.js:17010)
    at angular.js:16825
    at processQueue (angular.js:16843)
    at angular.js:16887
    at Scope.$digest (angular.js:17982) "Possibly unhandled rejection: {}"

请帮我解决这个问题,我搜索并阅读了大约 1 周,但无法解决此问题

【问题讨论】:

  • 你希望任何人都能在这个烂摊子中找到它?您需要将代码简化为重现问题的简洁示例。
  • 对不起我的烂摊子我尝试减少部分代码

标签: angularjs mongodb mean


【解决方案1】:

尝试删除res.header('("Content-Length", ""+json.length() );');

【讨论】:

  • carnet/export_countries/ 返回什么?
  • { _id: "58fdcd928816ed1112297592", Country_ISO: "AD", Country_Name_EN: "Andorra", Country_ID: 1845, Airport_ID: 2, Country_Name_DE: "Andorra", Country_Name_FR: "Andorre", Country_Name_IT: " Andorra", Country_Name_HU: "Andorra", Country_Name_PL: "Andorra", Country_Name_SK: "Andorra", Country_Name_CZ: "Andorra", Country_Name_ES: "Andorra", Country_Name_SE: "Andorra", Country_Name_NL: "Andorra", Country_Name_TR: "Andora" , Country_Name_DK: "Andorra", Country_Name_FI: "Andorra", Country_Name_RO: "Andora" },只需要 Country_Name_EN 那个索引
  • response.data.itemsundefined,所以response.data.items.length 会抛出Cannot read property 'length' of undefined 错误。它可以使用 GitHub api 工作,因为 GitHub 返回 {"items": [...]}
  • 如何解决这个未定义的错误?从服务器端还是在客户端?你能帮我吗?
  • 您想为国家/地区创建md-autocomplete,对吗?如果是这样,让服务器返回数组(使用find 而不是findOne),然后在客户端使用数组。
【解决方案2】:

请检查return$http.get是否写成一个单词(return 和 $http 之间的空格)并在响应时检查

if(response && response.data && response.data.items){
 return response.data.items
}
 return [];

【讨论】:

  • return$http.get 不是一个词,它有响应空间我现在检查没有任何错误长度,但也没有任何响应https://api.github.com/search/users 运行良好,只需在显示结果之前刷新整个页面
  • 我已经添加了聚合,我可以查看所有国家/地区,但由于 md-autocomplete: Could not resolve display value to a string. Please check the md-item-text` 属性出现错误,并且所有国家/地区都显示在自动完成 md-item-text="item.Country_Name_EN".then(function(response){ return response.data; 我所做的地方错误 ??? @Leibale Eidelman
猜你喜欢
  • 2017-01-10
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多