[ts]Left-hand side of assignment expression cannot be a constant or a readonly property


#1
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Contacts,ContactField } from 'ionic-native';
/*
  Generated class for the Addcontact page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/

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

 newcontact: any;

  constructor(private navCtrl: NavController) {
    this.navCtrl = navCtrl;
    this.newcontact = {
      displayName: '',
      nickname: '',
      phnumber: '',
      phtype: ''
    }

  }

  addcontact(newct) {
    let contact = Contacts.create();
    contact.displayName = newct.displayName;
    contact.nickname = newct.nickname;

    let contactfield = new ContactField();
    contactfield.type = newct.phtype;
    contactfield.value = newct.phnumber;
    contactfield.pref = true;

    var numbersection = [];
    numbersection.push(contactfield);
    contact.phoneNumbers = numbersection;

    contact.save().then((contact) => {
      alert('saved');
    }, (error) => {
      alert(error);
    })

  }
}

trying to develop a contacts app with ionic 2 with contacts plugin i keep getting [ts]Left-hand side of assignment expression cannot be a constant or a readonly property
anyone with pointers i will appreciate


#2

@simillustra The reason for the error is that the fields of the ionic-native Contact class are read-only:

export declare class Contact implements IContactProperties {
    private _objectInstance;
    readonly id: string;
    readonly displayName: string;
    readonly name: IContactName;
    readonly nickname: string;
    readonly phoneNumbers: IContactField[];
    readonly emails: IContactField[];
    readonly addresses: IContactAddress[];
    readonly ims: IContactField[];
    readonly organizations: IContactOrganization[];
    readonly birthday: Date;
    readonly note: string;
    readonly photos: IContactField[];
    readonly categories: IContactField[];
    readonly urls: IContactField[];
    constructor();
    clone(): Contact;
    remove(): Promise<any>;
    save(): Promise<any>;
}

Though I don’t know why, and it’s weird that in their documentation they assign a value to a read-only property:

import { Contact } from 'ionic-native';

let contact = new Contact();
contact.displayName = 'Mr. Ionitron';
contact.save().then(
  () => console.log('Contact saved!', contact),
  (error: any) => console.error('Error saving contact.', error)
);

I think the docs are not up-to-date to the current implementation. I’m using the latest framework (RC1).


#3

Then Should we open an issue with this case ?


#4

@mhartington About what is discussed above, related with the readonly Contact fields: is this an Ionic2 issue or are the docs not up-to-date?


#5

Did you find how resolve this problem ?


#6

This is working for me.

var contact = Contacts.create();
contact.phoneNumbers = [];
var cf = new ContactField('Mobile',item.Phone);
contact.phoneNumbers.push(cf);
contact.nickname = item.Title;
contact.name = item.Title;
contact.displayName = item.Title;

contact.save();

#7

For me the result is the same :frowning:


#8

I basically edited the contact.ts file and my app is working, guess it’s an
issue with ionic or docs not up to date

… Stay in eternity, Live in eternity, think differently, don’t get under
the pressure…


#10

export declare class Contact implements IContactProperties {
private _objectInstance;
id: string;
displayName: string;
name: IContactName;
nickname: string;
phoneNumbers: IContactField[];
emails: IContactField[];
addresses: IContactAddress[];
ims: IContactField[];
organizations: IContactOrganization[];
birthday: Date;
note: string;
photos: IContactField[];
categories: IContactField[];
urls: IContactField[];
constructor();
clone(): Contact;
remove(): Promise;
save(): Promise;
}
sorry it the contacts.d.ts file
this is how i edited it by removing the readonly attribute i got it working


#11

@simillustra That seems a bit hacky, but if it make it works for now better than nothing :slight_smile:

And I think the Ionic team will take care of that soon, so for now something like that may help.


#12

I hope so too, currently am trying to access the phone contact variables
like phone numbers and email but have not made much progress, do you have
any pointers?
my code
<ion-list [hidden]="!search"> <ion-item *ngFor="let item of contactListArray ; let i = index"> <ion-avatar item-left> <img src="http://ionicframework.com/dist/preview-app/www/assets/img/avatar-finn.png"> </ion-avatar> <ion-label stacked>{{item.displayName}}</ion-label> <ion-input type="text" value="{{item.displayName}}"></ion-input> <p><strong>Phone(s)</strong></p> <ion-item *ngFor="let number of item.phoneNumbers; let j = index"> <p>{{ number.value }}</p> </ion-item> <p><strong>Email(s)</strong></p> <ion-item *ngFor="let email of item.emails; let k = index"> <p>{{ email.value }}</p> </ion-item> </ion-item> </ion-list>

contlist.ts
getContactList() {

    let options = new ContactFindOptions();
    options.filter = '';
    options.multiple = true;
    options.desiredFields = ['displayName', 'phoneNumbers', 'emails'];
    options.hasPhoneNumber = true;


    Contacts.find(['*'], options).then((contact) => {
        this.contactListArray = contact;
        console.log(this.contactListArray); // This shows the complete object.      
        
        //pushit to contact view point
    }, (error) => {
        let toast = this.toastCtrl.create({
            message: 'Error Loading Contact List. Please Try Again',
            duration: 3000,
            position: 'bottom'
        });
        toast.present();
    })
}

… Stay in eternity, Live in eternity, think differently, don’t get under
the pressure…
http://goo.gl/Bggtzg


#13

@simillustra Sorry, I’m not using contact information for my app in the moment, so I haven’t implemented anything related to contacts yet :confused:


#14

I have the same issue~~~~~


#15

hi guys! worked for me ->>>

 let contact = (<any>Contacts).create();
      contact.name = new ContactName(null, 'Smith', 'John');
      contact.phoneNumbers = [new ContactField('mobile', '+553185763389')];
      contact.save().then(
      () => console.log('Contact saved!', contact),
      (error: any) => console.error('Error saving contact.', error)