2017-04-27 118 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 –