Array of my model is send TypeError undefined in Angular 6

javascript undefined error
typeerror: cannot read property of undefined
cannot read property of undefined typescript
javascript array undefined
error typeerror: cannot read property of undefined angular 6
typeerror is undefined
error typeerror cannot read property 'value' of undefined angular 6
error typeerror: cannot read property of undefined angular 8

I'm beginner in Angular (I use version 6). For improving my experience I've created a little web application. So, I have an error which I don't understand. So, I have a model called "Spot" :

export class Spot {
    photo: string;

    title: string;
    descri: string;
    type: string;
    lat: number;
    lng: number;

    constructor(public titleSpot: string,
                public description: string,
                public typeSpot: string,
                public latitude: number,
                public longitude: number,
            ) {
        this.title = titleSpot;
        this.descri = description; 
        this.type = typeSpot;        
        this.lat = latitude;  
        this.lng = longitude; 
    }
}

The error is beacause of, you are using javascript function in forEach and in javacript function this will not work, so instead of normal function you have to use fat arrow function

here's is an example,

change is here

snapshot.forEach((childSnapshot) => { });

public spots: Spot[] = [];

getSpots() {
   return new Promise(
     (resolve, reject) => {
       firebase.database().ref('/spots/').once('value').then(
         (snapshot) => {
           snapshot.forEach((childSnapshot) => {
             var key = childSnapshot.key;
             var childData = childSnapshot.val();
          console.log(childData.titleSpot);
          console.log(childData.description);
          console.log(childData.typeSpot);
          console.log(childData.latitude);
          console.log(childData.longitude);

          this.spots.push(new Spot(childData.titleSpot,
           childData.description, childData.typeSpot, childData.latitude, childData.longitude));
           });
           resolve(snapshot.val());
         }, (error) => {
           reject(error);
         }
       );
     }
   );
 }

html, The error is beacause of, you are using javascript function in forEach and in javacript function this will not work, so instead of normal function  🐞 bug report Affected Package The issue is caused by package @angular/forms Is this a regression? No, example stackblitz is using v6, discovered bug in v8 Description Attempting to patchValue a FormArray with the value undefined results


You need to initialize spots with an empty array,

public spots: Spot[] = [];

7 Tips to Handle undefined in JavaScript, The JavaScript interpreter returns undefined when accessing a let array = null; array; // => null let movie = { name: 'Starship TypeError: 'undefined' is not a function; TypeError: Cannot read property '<prop-name>' of undefined; and From 6 primitive types undefined is a special value with its own type  This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem. Read more about our automatic conversation locking policy.


Edited my answer.

Your class Spot have the following:

photo: string;
title: string;
descri: string;
type: string;
lat: number;
lng: number;

To instantiate your variable spot as array, you need to do this:

public spot: Spots[] = [];

spot.push({photo: "", title: "", descri: "", type: "", lat: 0, lng: 0  });

[Angular] TypeError: Cannot read property 'value' of undefined , Describe the bug When using a decorator to provide required modules to an angular component, storybook throws the error below: TypeError: Cannot at Array.some (<anonymous>) at getExistenceOfComponentInModules (VM82 We are always open to contributions so please send us a pull request if  javascript java c# python android php jquery c++ html ios css sql mysql.net c r asp.net ruby-on-rails objective-c arrays node.js sql-server iphone regex ruby angularjs json swift django linux asp.net-mvc xml wpf angular spring string ajax python-3.x git excel windows xcode multithreading pandas database reactjs bash scala algorithm eclipse


I would like to correct few things in your code:

When you use public propName in constructor, it will create new property on that class and assign it that value, therefor, your Spot class has every property duplicated. Here is correct way

export class Spot {
    photo: string;

    constructor(public title: string,
            public description: string,
            public type: string,
            public lat: number,
            public lng: number,
    ) {
        //  other code...
    }
}

is same as

export class Spot {
    photo: string;

    public title: string
    public description: string
    public type: string
    public lat: number
    public lng: number

    constructor(title: string, description: string, type: string, lat: number, lng: number) {
        this.title = title;
        this.description= description;
        this.type= type;
        this.lat= lat;
        this.lng= lng;

        //  other code...
    }
}

Your class that implements getSpots will need to have this too: public spots: Spot[].

Use const when you never reasign the value, as it is better for performance and readability. firebase.database().ref('/spots/').once('value') returns promise by itself, therefor no need to wrap it.

getPosts() {
    return firebase.database().ref('/spots/').once('value').then(snapshot => {
        return Array.from(snapshot).map(childSnapshot => {
            const key = childSnapshot.key;
            const childData = childSnapshot.val();

            return new Spot(childData.titleSpot, childData.description, childData.typeSpot, childData.latitude, childData.longitude);
         });
    );
)

TypeError: "x" has no properties, The JavaScript exception "null (or undefined) has no properties" occurs when you attempt TypeError: Unable to get property {x} of undefined or null reference (Edge) TypeError: null has Last modified: May 6, 2020 , by MDN contributors RangeError: argument is not a valid code point · RangeError: invalid array length​  These steps worked for me: 1)Clear package.json with the importat packages for example I have delete my package and insert one by one here is my


Array.prototype.indexOf(), The indexOf() method returns the first index at which a given element can be found in the 6. // start from index 2. 7. console.log(beasts.indexOf('bison', 2));. 8 if (this === null || this === undefined) throw TypeError("Array.prototype. Let o be the result of calling ToObject passing // the this value as the  Array.prototype.reduce(), Array.prototype.reduceRight() and TypedArray.prototype.reduce() , TypedArray.prototype.reduceRight() ). These functions optionally take an initialValue (which will be used as the first argument to the first call of the callback ).


Uncaught TypeError: Cannot read property 'name' of undefined , This works because the && operators actually return one of the value of deepProp returns undefined because obj.prop1 is undefined which will property argument will be evaluated when we pass it into the function. In Angular, there is the support elvis operator to protect against a view render failure​. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.


angular.forEach, v1.6.6, v1.6.5, v1.6.4, v1.6.3, v1.6.2, v1.6.1, v1.6.0, v1.6.0-rc.2, v1.6.0-rc.1, v1.​6.0-rc.0 Invokes the iterator function once for each item in obj collection, which can be key, obj) , where value is the value of an object property or an array element, providing 'undefined' or 'null' values for obj will not throw a TypeError, but  Maybe the issue is caused by package @angular/cli because it's happens when I update Angular from 6.1 to 8. Is this a regression? Yes, the previous version in which this bug was not present was: 6.1. Description. After update when npm start I get this error: ERROR in Expected array when reading property declarations 🔬 Minimal Reproduction