I need to save some usernames (input) and pictures (another button not shown in the code but the logic will be the same I think) in my Ionic 3 app without classic dataBase (think I'll use the base 64 for the pictures based on the camera from the device). Then I should retrieve them to create ion-card profile with it.
Actually everytime I run my loadUser() function it only retrieve me the last username but I would like to save more. Even when I go in the chrome DevTools/IndexedDB I can see only one key and value saved.
I need to persist theses key and value just like if is a database.
Using an input with NgModel and a 2 buttons (one to set data and one to get data) I am trying to save some username in my Ionic 3 app (without Sql Lite) but I see that the storage.set only accept string value. My idea was to use JSON.parse to retrieve the datas save in an array but the value is string at the begginning so if you have any idea let me know.
Bellow is the code I tried but I get an error: "core.js:1449 ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token d in JSON at position 0
SyntaxError: Unexpected token d in JSON at position 0
at JSON.parse ()"
PS: If it is not clear enough to you feel free to ask I am around for a while. Here is my code:
parameter.ts
export class ParametrePage {
key: string = 'username';
inputext: string;
inputextGroup = [];
constructor(
public navCtrl: NavController,
public navParams: NavParams,
private storage: Storage
) {}
saveUser() {
// set a key/value
this.storage.set(this.key, this.inputext);
}
loadUser() {
// Or to get a key/value pair
this.storage.get(this.key).then(val => {
this.inputtextGroup = JSON.parse(val);
console.log('You name is', val);
});
}
}
parameter.html:
<h6>Add a profilname</h6>
<div class="form-container">
<div class="input-container">
<p class="diName">Name :</p>
<ion-input [(ngModel)]="inputext" class="daInput" type="text"></ion- input>
</div>
<button class="charlotte-button addUser" ion-button icon-left
(click)="saveUser()">
<ion-icon class="picto picto-reply"></ion-icon>
Add a user
</button>
<button class="charlotte-button addUser" ion-button icon-left
(click)="loadUser()">
<ion-icon class="picto picto-reply"></ion-icon>
Load user
</button>