【问题标题】:Typescript Error Property 'text' does not exist on type 'Object'Typescript 错误属性“文本”在“对象”类型上不存在
【发布时间】:2018-08-08 02:29:25
【问题描述】:

我正在数据库中进行编辑,出现打字稿错误 2339 这是代码

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()) 这部分返回错误。我试图改变它并使它成为 .map ((res: Response) => res.text()) 然后会提示另一个错误,那就是 this.sqlitePorter.importSqlToDb(this.database, sql) 错误is " Promise 类型的参数不能分配给 类型的参数。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    使用HttpClient,您不再需要映射Http 调用的结果,因为它默认返回结果而不是响应对象。如果您的 http 调用的结果已经是一个字符串,那么只需删除 .map 行。即

    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);
            })
        });
    }
    

    【讨论】:

    • 我删除了它下一个错误是这一行中的承诺字符串 this.sqlitePorter.importSqlToDb(this.database, sql) 它突出显示了 sql。
    • @RonaCeciliaAbayata,我更新了我的答案。您的get 调用应指定对象的返回类型,在本例中为string,否则默认为object
    • 谢谢。第一个错误已解决。现在我的问题出在这一行上,this.sqlitePorter.importSqlToDb(this.database, sql) 它返回此错误(Typescript Error Argument of type 'Object' is notassignable to parameter of type'string')
    • 您是否将呼叫更新为this.http.get&lt;string&gt;?如果这不起作用(应该有),请将您的订阅行更新为 .subscribe((sql: string) =&gt; {
    • 非常感谢。它不再返回错误。
    猜你喜欢
    • 2023-02-21
    • 2020-08-07
    • 2021-06-19
    • 2019-04-12
    • 2020-07-09
    • 2019-10-20
    • 2021-09-26
    • 2018-07-16
    • 1970-01-01
    相关资源
    最近更新 更多