2017-04-27 119 views
0

我在Angularjs和MongoDB中创建新的m我需要从MongoDB发送HTTP请求以用于自动完成角度材质,当我发送到git hub API时它完美地工作,但对于我的数据库, '不能读取属性 '' 的未定义' 我控制器长度客户端Angularjs 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周左右,但解决不了这个问题

+2

您如何期待任何人在此混乱中找到它?你需要把你的代码烧写成一个简洁的例子来重现问题。 –

+0

对不起我的混乱我试图减少一部分代码 –

回答

0

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

+0

我删除已经得到相同的错误 –

+0

什么'carnet/export_countries /'返回? –

+0

{ _id: “58fdcd928816ed1112297592”, Country_ISO: “AD”, Country_Name_EN: “安道尔”, COUNTRY_ID:1845, Airport_ID:2, Country_Name_DE: “安道尔”, Country_Name_FR: “安道尔”, Country_Name_IT: “安道尔”, Country_Name_HU: “安道尔”, Country_Name_PL: “安道尔”, Country_Name_SK: “安道尔”, Country_Name_CZ: “安道尔”, Country_Name_ES: “安道尔”, Country_Name_SE: “安道尔”, Country_Name_NL:“安道尔“, Country_Name_TR:”Andora“, Coun try_Name_DK: “安道尔”, Country_Name_FI: “安道尔”, Country_Name_RO: “安道尔” }, 只需要Country_Name_EN该指数 –

0

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

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

'返回$ http.get'是不是一个字它的空间 为响应我现在检查没有任何错误长度但没有任何响应以及 'https:// api.github.com/search/users' 在显示结果前只刷新整个页面 –

+0

我已经加入聚合了我可以看到所有的国家,但是出现'md-autocomplete:无法解析显示值赋给一个字符串。请检查“md-item-text”属性,并且所有国家/地区都显示在自动完成的'md-item-text =“item.Country_Name_EN”'和'.then(功能(响应){0128} “我错了吗?@Leibale Eidelman –