0

I have a problem with my code. I have no error when I compile my code, but I have a console error: this.trackArray[0].points.getVector() is not a function.

Here is my code where I call this function

const vecteurPoints: THREE.Vector3[] = this.trackArray[0].points.getVector();

Here is the function

public getVector(): Vector3[] {
    const vecArray: Vector3[] = new Array();
    for (const i of this.coordX) {
        vecArray[i].x = this.coordX[i];
        vecArray[i].y = this.coordY[i];
        vecArray[i].z = this.coordZ[i];
    }

    return vecArray;
}

In VS Code, when I ctrl+click on the function it sends me to the function definition.

I get my trackArray with a call to an API.

export class AdminService {
    public constructor( private http: HttpClient ) {  }

    public getTracks(): Observable<Track[]> {
        return this.http.get<Track[]>(this.ADMIN_URL).pipe(catchError(this.handleError<Track[]>("getTracks"))
        );
 }

And I call this class like this

 this.admin.getTracks().subscribe((track: Track[]) => {this.trackArray = track; });
2
  • 1
    Have you verified that this.trackArray[0] exists? Commented Apr 2, 2018 at 1:51
  • 1
    Also, the getVector() function is problematic, because it tries to set properties on vecArray[i], when vecArray[i] hasn't been created yet Commented Apr 2, 2018 at 1:54

2 Answers 2

1

I found my problem! Because I got the trackArray with JSON, I had to instantiate a new track to apply my method! Thank you all for your help

Sign up to request clarification or add additional context in comments.

Comments

0

This could happen if this.trackArray does not contain elements. Add a null check

if(this.trackArray && this.trackArray[0]){
  const vecteurPoints: THREE.Vector3[] = this.trackArray[0].points.getVector();
}

2 Comments

If we know that trackArray is an array, and we know that each item in it has a points property, I think the only thing we should have to check is if (this.trackArray[0])
I used a console.log and the track have an element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.