0

I am editing in the database and there was a typescript error 2339 Here is the code

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Platform } from 'ionic-angular';
import { SQLite, SQLiteObject} from '@ionic-native/sqlite'; 
import { SQLitePorter } from '@ionic-native/sqlite-porter'
import { BehaviorSubject } from 'rxjs/Rx';
import 'rxjs/add/operator/map';

/*
  Generated class for the DatabaseProvider provider.

      See https://angular.io/guide/dependency-injection for more info on                     
providers
  and Angular DI.
*/
@Injectable()
export class DatabaseProvider {
    database: SQLiteObject;
    private dbReady: BehaviorSubject<boolean>;

  constructor(public http: HttpClient, private sqlitePorter: SQLitePorter,             private storage: Storage, private sqlite: SQLite, private platform: Platform)     
{
    this.dbReady = new BehaviorSubject(false);
    this.platform.ready().then(() => {
        this.sqlite.create({
            name: 'assessment.db',
            location: 'default'
        })
        .then((db:SQLiteObject) => {
            this.database = db;
            this.storage.get('database_filled').then(val => {
                if(val){
                    this.dbReady.next(true);
                }
                else{
                    this.fillDatabase();
                }
            })
        });
    });  
}

fillDatabase(){
    this.http.get('assets/test.sql')
    .map (res => res.text())
    .subscribe(sql => { 
        this.sqlitePorter.importSqlToDb(this.database, sql)
        .then(data => {
            this.dbReady.next(true);
            this.storage.set('database_filled', true);
        })
    });
}

  getDatabaseState(){
    return this.dbReady.asObservable();
  }

}

.map (res => res.text()) this part returns an error. I have tried to alter this and made it .map ((res: Response) => res.text()) then another error would prompt and that is in the line this.sqlitePorter.importSqlToDb(this.database, sql) the error is " Argument of type Promise is not assignable to parameter of type .

1 Answer 1

1

With HttpClient, you no longer need to map the result of a Http call as it returns the result by default rather than the response object. If the result of your http call is already a string then just remove the .map line. i.e.

fillDatabase(){
    this.http.get<string>('assets/test.sql')
    .subscribe(sql => { 
        this.sqlitePorter.importSqlToDb(this.database, sql)
        .then(data => {
            this.dbReady.next(true);
            this.storage.set('database_filled', true);
        })
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

i deleted it the next error is the promise string in this line this.sqlitePorter.importSqlToDb(this.database, sql) it highlighted sql.
@RonaCeciliaAbayata, I updated my answer. Your get call should specify the return type of the object, which in this case is string, otherwise it gets defaulted to be object.
thank you. the first error was solved. now my problem is with this line, this.sqlitePorter.importSqlToDb(this.database, sql) it returns this error (Typescript Error Argument of type 'Object' is not assignable to parameter of type 'string'.)
Did you update your call to be this.http.get<string>? If this didn't work, which it should have, update your subscribe line to be .subscribe((sql: string) => {
thank you so much. it did not return an error anymore.

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.