离子框架将数据添加到阵列
问题描述:
firedata = firebase.database().ref('/chatusers/');
postData(val){
items=[];
this.items.push(val);
this.firedata.child(this.afireauth.auth.currentUser.uid).update({
uid: this.afireauth.auth.currentUser.uid,
post: this.items,
displayName: this.afireauth.auth.currentUser.displayName
})
})
我想我的数据推到火力点,所以每次的funtion POSTDATA叫我希望我的阵列items=[]
跟上数据val
追加。 目前它刚刚覆盖新的价值。 我是ionic3
的新用户,请帮助我。离子框架将数据添加到阵列
expected output:
---post
0: 'A'
1: 'B'
2: 'C'
uid:
-----------------------------------------------
Code with ionic3 storage
firedata = firebase.database().ref('/chatusers/');
postData(val){
var uniqueUID = this.afireauth.auth.currentUser.uid;
let toaster = this.toastCtrl.create({
duration: 3000,
position: 'bottom'
});
if(this.userdata.title==""){
toaster.setMessage("Please fill out the title");
toaster.present();
}
this.storage.get('Task1-'+uniqueUID).then((item)=> {
if (item != null) {
item.push(val);
this.storage.set('Task1-'+uniqueUID, item);
}
else {
let array = [];
array.push(val);
this.storage.set('Task1-'+uniqueUID, array);
}
this.items.push(val);
this.firedata.child(this.afireauth.auth.currentUser.uid).update({
uid: this.afireauth.auth.currentUser.uid,
post: item,
displayName: this.afireauth.auth.currentUser.displayName
})
})
}
}
答
只需在myMethod()
之外声明它,如下所示。
items:any=[];
constructor(){
}
myMethod(){
firedata = firebase.database().ref('/chatusers/');
postData(val){
this.items.push(val);
this.firedata.child(this.afireauth.auth.currentUser.uid).update({
uid: this.afireauth.auth.currentUser.uid,
post: this.items,
displayName: this.afireauth.auth.currentUser.displayName
})
})
}
不起作用。所以基本上,当我切换到其他选项卡它只是覆盖。不要按照我想要的Firebase列表方式追加列表。 – unknown
哦..你没有告诉页面移动没有?您需要使用Ionic Storage模块。请参阅并告诉我您是否需要更多帮助:http://ionicframework.com/docs/storage/ – Sampath
我做了存储部分,它确实有效,但问题是什么遇到的是相同的数据被存储为所有的用户。我已经发布了离子存储的代码。检查 – unknown