0

I,m new in Angularjs and MongoDB I need to send HTTP request from MongoDB for auto complete angular material when I send to git hub API it's working perfectly but for my DB, I get 'Cannot read property 'length' of undefined' my controller in client side

    /*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;
            });

        };
      });

})();

my controller for my 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;

and index.js server side :

//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();
});

my app.js

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

my error :

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: {}"

please help me in this matter I search and read around 1 week but cannot solve this problem

Mayur Shah
  • 3,344
  • 1
  • 22
  • 41
John Doe
  • 55
  • 1
  • 10

2 Answers2

0

Try to remove res.header('("Content-Length", ""+json.length() );');

Leibale Eidelman
  • 2,772
  • 1
  • 17
  • 28
  • What does `carnet/export_countries/` returns? – Leibale Eidelman Apr 27 '17 at 05:50
  • { _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: "Andora", 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" }, need only Country_Name_EN that index – John Doe Apr 27 '17 at 06:45
  • `response.data.items` is `undefined`, so `response.data.items.length` will throw `Cannot read property 'length' of undefined` error. It's works using GitHub api because GitHub returns `{"items": [...]}`. – Leibale Eidelman Apr 27 '17 at 07:48
  • How can fix this undefined error? from server side or in client side? Could you help me please ? – John Doe Apr 27 '17 at 07:59
  • You want to create `md-autocomplete` for countries, am i right? If so make the server return array (use `find` instead of `findOne`) and then use the array in the client. – Leibale Eidelman Apr 27 '17 at 08:03
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/15962398) – Oleg Estekhin Apr 27 '17 at 08:57
  • I really want to do that but I try all the way but still not get any result mean while I change the code `this.find({_id: Country_Name_EN}, function(error, CarnetExportCountries)` but still nothing – John Doe Apr 27 '17 at 09:17
  • @OlegEstekhin i thought that this was the problem. – Leibale Eidelman Apr 27 '17 at 09:29
  • @JohnDoe http://stackoverflow.com/questions/26814456/how-to-get-all-the-values-that-contains-part-of-a-string-using-mongoose-find – Leibale Eidelman Apr 27 '17 at 09:29
  • I change server side to this `this.find({"Country_Name_EN": {"$regex":"[]"}}, function(error, CarnetExportCountries){ if (error) { // Throw an error res.send(error); } res.json(CarnetExportCountries); });` and for client side change to this: `this.querySearch = function(query){ return $http.get("http://localhost:3000/carnet/export_countries/", { params: { q: query } }).then(function(Country_Name_EN){ return Country_Name_EN; }); }; });` I don't get error anymore but nothing work as well – John Doe Apr 27 '17 at 10:52
0

Please check if return$http.get written as one word (whitespace between return and $http) and make a check at response

if(response && response.data && response.data.items){
 return response.data.items
}
 return [];
  • `return$http.get` is not one word its with space for response I check now no any error length but not any response as well `https://api.github.com/search/users` working well just refresh whole page before show result – John Doe Apr 27 '17 at 07:52
  • I add aggregate already I can get all countries in view but get error as `md-autocomplete: Could not resolve display value to a string. Please check the `md-item-text` attribute` and all countries show at autocomplete `md-item-text="item.Country_Name_EN"` and `.then(function(response){ return response.data;` where I did mistake ??? @Leibale Eidelman – John Doe May 02 '17 at 10:02