1

when i need to access to this ,

but i found problem to give the value true to this.idHiden

like : this.idHiden = true the error :

TypeError: Cannot set property 'idHiden' of null

and the code

import { Component  , Output} from '@angular/core' ;
import { NgForm }     from '@angular/forms';
 import * as Datastore from 'nedb';

@Component({
  moduleId : module.id ,
  providers: [ ],
  templateUrl : 'info.component.html'
})

export class InfoComponent {
  selkName : any ;
  insertedSelk : any  ;
  idHiden : false ;

  selkValidate : number ;

  constructor(){
    this.selkName = '' ;
    this.insertedSelk =  [] ;
    this.selkValidate = 1 ;
    this.SelkFinde(this) ;

  }
  DeletSelk(id:number ){
    let db = new Datastore({filename : 'ComerceDB'});
    db.loadDatabase(function() {
      db.remove({ _id: id }, {}, function (err:any, numRemoved:any) {

        this.idHiden = true
        console.log(this.idHiden) ;
      });
    });


}
}
1

1 Answer 1

2
db.loadDatabase(function() {
  db.remove({ _id: id }, {}, function (err:any, numRemoved:any) {

should be

db.loadDatabase(() => {
  db.remove({ _id: id }, {}, (err:any, numRemoved:any) => {

otherwise this will point to the caller instead to the current class.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

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

6 Comments

There is also a function in the outer function (loadDatabase)
Thanks, totally missed that. Had to skim through the code 3x, even after I saw your comment :D. Thanks for the hint!
can you explain to me
Not sure what to explain. If you use this.xxx inside a function, then that works only if this. actually points to where you expect. Even if you are using TypeScript, a language with classes, it's still JavaScript what you are working with (with some additional syntactic sugar), and in JS this by default points to the scope where it was called. When you pass the function to loadDatabase or remove then when it is called from there this will point to loadDatabase or remove, not InfoComponent
That's not necessary if you consequently use ()=>
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.