I am trying to implement user like/unlike function in ionic 2. The idea is simply to click the like button to toggle like/unlike status.
我试图在离子2中实现用户喜欢/不同的功能。想法只是点击类似按钮来切换/不同状态。
likeItem(itemId) {
let objRef = this.af.database.object('userItemCollection/'+this.userId+'/items/'+itemId);
objRef.subscribe(snapshot => {
if(snapshot.$value) {
objRef.remove();
} else {
objRef.set(true);
}
});
}
However, once I click the like button and trigger the function, I can see in the Firebase console, it adds many records to the database. I am not sure where I go wrong.
但是,一旦我单击“喜欢”按钮并触发该功能,我可以在Firebase控制台中看到它向数据库添加了许多记录。我不知道哪里出错了。
1 个解决方案
#1
2
You can do this by using the Observable take method::
您可以使用Observable take方法执行此操作::
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/take'
likeItem(itemId) {
let objRef = this.af.database.object('/item/itemId', { preserveSnapshot: true })
objRef.take(1)
.subscribe(snapshot => objRef.set({like: !snapshot.val().like}))
}
I hope this solves your question :)
我希望这能解决你的问题:)
#1
2
You can do this by using the Observable take method::
您可以使用Observable take方法执行此操作::
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/take'
likeItem(itemId) {
let objRef = this.af.database.object('/item/itemId', { preserveSnapshot: true })
objRef.take(1)
.subscribe(snapshot => objRef.set({like: !snapshot.val().like}))
}
I hope this solves your question :)
我希望这能解决你的问题:)