How to preserve data in array from Ionic - this.array.push is not a function
up vote
0
down vote
favorite
The array storedArr = is used to store data using storage, however, I receive the .push is not a function when I try to fill it with the storage get method:
storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;
The esencial part of my code is:
import { Storage } from '@ionic/storage';
export class MyPage {
constructor(
private storage: Storage) {
}
// storedArr = ; This works but resets the array
storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
How should I write that part of the code?
arrays angular typescript ionic-framework storage
add a comment |
up vote
0
down vote
favorite
The array storedArr = is used to store data using storage, however, I receive the .push is not a function when I try to fill it with the storage get method:
storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;
The esencial part of my code is:
import { Storage } from '@ionic/storage';
export class MyPage {
constructor(
private storage: Storage) {
}
// storedArr = ; This works but resets the array
storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
How should I write that part of the code?
arrays angular typescript ionic-framework storage
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The array storedArr = is used to store data using storage, however, I receive the .push is not a function when I try to fill it with the storage get method:
storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;
The esencial part of my code is:
import { Storage } from '@ionic/storage';
export class MyPage {
constructor(
private storage: Storage) {
}
// storedArr = ; This works but resets the array
storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
How should I write that part of the code?
arrays angular typescript ionic-framework storage
The array storedArr = is used to store data using storage, however, I receive the .push is not a function when I try to fill it with the storage get method:
storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;
The esencial part of my code is:
import { Storage } from '@ionic/storage';
export class MyPage {
constructor(
private storage: Storage) {
}
// storedArr = ; This works but resets the array
storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
How should I write that part of the code?
arrays angular typescript ionic-framework storage
arrays angular typescript ionic-framework storage
edited 16 hours ago
marvinIsSacul
25014
25014
asked Nov 16 at 14:29
Ricardo Castañeda
2,93152137
2,93152137
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.
So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it assigns . Hence the error - because Promise.prototype does not contain a push method.
In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...
export class MyPage {
storedArr = ;
constructor(private storage: Storage) {
this.storage.get('stored')
.then(data => {
this.storedArr = data;
});
}
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;
– Ricardo Castañeda
38 mins ago
add a comment |
up vote
0
down vote
If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.
var storageResult = await this.storage.get('stored');
storedArr = (storageResult) ? JSON.parse(storageResult) : ;
Additionally, when you store the array you likely want to do a JSON.stringify on it.
this.storage.set('stored', JSON.stringify(this.storedArr));
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.
So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it assigns . Hence the error - because Promise.prototype does not contain a push method.
In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...
export class MyPage {
storedArr = ;
constructor(private storage: Storage) {
this.storage.get('stored')
.then(data => {
this.storedArr = data;
});
}
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;
– Ricardo Castañeda
38 mins ago
add a comment |
up vote
1
down vote
The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.
So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it assigns . Hence the error - because Promise.prototype does not contain a push method.
In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...
export class MyPage {
storedArr = ;
constructor(private storage: Storage) {
this.storage.get('stored')
.then(data => {
this.storedArr = data;
});
}
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;
– Ricardo Castañeda
38 mins ago
add a comment |
up vote
1
down vote
up vote
1
down vote
The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.
So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it assigns . Hence the error - because Promise.prototype does not contain a push method.
In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...
export class MyPage {
storedArr = ;
constructor(private storage: Storage) {
this.storage.get('stored')
.then(data => {
this.storedArr = data;
});
}
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.
So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it assigns . Hence the error - because Promise.prototype does not contain a push method.
In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...
export class MyPage {
storedArr = ;
constructor(private storage: Storage) {
this.storage.get('stored')
.then(data => {
this.storedArr = data;
});
}
saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});
this.storage.set('stored', this.storedArr);
}
}
answered 18 hours ago
marvinIsSacul
25014
25014
Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;
– Ricardo Castañeda
38 mins ago
add a comment |
Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;
– Ricardo Castañeda
38 mins ago
Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;
– Ricardo Castañeda
38 mins ago
Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;
– Ricardo Castañeda
38 mins ago
add a comment |
up vote
0
down vote
If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.
var storageResult = await this.storage.get('stored');
storedArr = (storageResult) ? JSON.parse(storageResult) : ;
Additionally, when you store the array you likely want to do a JSON.stringify on it.
this.storage.set('stored', JSON.stringify(this.storedArr));
add a comment |
up vote
0
down vote
If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.
var storageResult = await this.storage.get('stored');
storedArr = (storageResult) ? JSON.parse(storageResult) : ;
Additionally, when you store the array you likely want to do a JSON.stringify on it.
this.storage.set('stored', JSON.stringify(this.storedArr));
add a comment |
up vote
0
down vote
up vote
0
down vote
If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.
var storageResult = await this.storage.get('stored');
storedArr = (storageResult) ? JSON.parse(storageResult) : ;
Additionally, when you store the array you likely want to do a JSON.stringify on it.
this.storage.set('stored', JSON.stringify(this.storedArr));
If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.
var storageResult = await this.storage.get('stored');
storedArr = (storageResult) ? JSON.parse(storageResult) : ;
Additionally, when you store the array you likely want to do a JSON.stringify on it.
this.storage.set('stored', JSON.stringify(this.storedArr));
edited Nov 16 at 14:59
answered Nov 16 at 14:50
BRass
1,090618
1,090618
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339804%2fhow-to-preserve-data-in-array-from-ionic-this-array-push-is-not-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
