Problem Typescript Error Type 'Object' is not assignable to type 'string'

1

I am trying to send an information with ajax that is the following

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Proveedor1Provider } from '../../providers/proveedor1/proveedor1';
import { AboutPage } from '../about/about';
import { LoadingController } from 'ionic-angular';
import { ToastController } from 'ionic-angular';

@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {

    usuarios="";

    constructor(public toastCtrl: ToastController,
          public navCtrl: NavController,
          public provedor: Proveedor1Provider,
          public loadingCtrl: LoadingController) {}

    openNavDetailsPage(item) {
        this.navCtrl.push(AboutPage, { item: item });
    }
    presentToast(val) {
        const toast = this.toastCtrl.create({
         message: 'The character '+val+' is not registered',
         duration: 3000
        });
        toast.present();
    }
    getItems(ev) {

        let loading = this.loadingCtrl.create({
            content: 'Please wait...'
        });
        var val = ev.target.value;
        if(val.length > 1){
            loading.present();
            this.provedor.obtenerDatos(val)
            .subscribe(
                (data)=> {if(data != undefined){this.usuarios = data;}else{this.presentToast(val);this.usuarios = '';}},
                (error)=> {console.log(error);},
                ()=>{loading.dismiss();}
                )
        }
    }
}

The error that I get is that this.users in the part of

(data)=> {
  if(data != undefined){this.usuarios = data;} 
  else{this.presentToast(val);this.usuarios = '';}}

is the following

  

Typescript Error: Type 'Object' is not assignable to type 'string'.

    
asked by Emmanuel Lorenzo 26.07.2018 в 09:25
source

2 answers

0

It must be because data is an Array Object or Object, you can transform it with data.toString () or verify its values with console.log();

One way to access data in a json array is with [] Ex:

data[0][0]['nombre_campo'];

    
answered by 26.07.2018 в 10:22
0

The error comes up because Typescript (the compiler) is assuming that data is not a String. That means that this.provedor.obtenerDatos(val) (there is an erratum, I guess it's prove e dor) returns an Observable where T is not a string.

You will have to check the signature of that method to solve the problem.

    
answered by 26.07.2018 в 11:15