I'm making an app with Angular 2 and Electron and using NeDB as a data storage.
The search service is intended to handle operations with DB, for now I want it to load whole database and return it.
search.service.ts
import { Injectable } from '@angular/core';
import * as Datastore from 'nedb';
import * as path from 'path';
@Injectable()
export class SearchService {
constructor() { }
getData(){
var db = new Datastore( {
filename: path.resolve('src/assets/db.json'),
autoload: true,
});
var result;
db.find({}, function(err, docs) {
result = docs;
});
console.log(result);
return result;
}
}
In my component I use a method to call getData() method of service upon initialization of component with ngOnInit hook.
But when I log results I get undefined
.
While tinkering with search ervice I found out that docs
I have in find()
method are not accessible from outside the method and it seems like result = docs
does nothing. So I was thinking that I need some async magic to make this work. I wanted to do this with Observables but couldn't achieve anything, probably was doing it incorrectly.