[英]List of data with strange behavior when update firebase value on VueJS
I'm having a really strange behavior on the list of data when I try to update a value on firebase. 当我尝试更新Firebase上的值时,我在数据列表上有一个非常奇怪的行为。
I've putted an example of my code here: https://codepen.io/Raulmo/pen/BRMWNM?editors=1111 我在这里放了一个代码示例: https ://codepen.io/Raulmo/pen/BRMWNM?editors =1111
Here is my vueJS: 这是我的vueJS:
// Setup Firebase
let config = {
databaseURL: "https://test-stack-de2e3.firebaseio.com"
};
let firebaseapp = firebase.initializeApp(config);
let db = firebaseapp.database();
let postsRef = db.ref('blog/posts')
// create Vue app
var app = new Vue({
// element to mount to
el: '#app',
// initial data
data: {
posts: [],
newPost: {
title: '',
content: '',
story: ''
}
},
// methods
methods: {
addPost: function () {
postsRef.push(this.newPost);
this.newPost.title = '';
this.newPost.content = '';
this.newPost.story = '';
},
editPost: function(post) {
// Set post values to form
this.newPost = post
},
updatePost: function(post) {
post.title = "changed"
const childKey = post['.key'];
/*
* Firebase doesn't accept speacial chars as value
* so delete `.key` property from the post
*/
delete post['.key'];
/*
* Set the updated post value
*/
this.$firebaseRefs.posts.child(childKey).set(post)
},
removePost: function (post) {
postsRef.child(post['.key']).remove()
toastr.success('Business removed successfully')
},
},
// Explicitly set binding data to firebase as an array.
created() {
this.$bindAsArray('posts', postsRef);
}
})
What is happening is that every time a press the edit button i've forced the value of post.title to change to another value and then update it on firebase, but when I do so on the first element it also changes the value of the last element of the list. 发生的情况是,每按一次编辑按钮,我都会将post.title的值强制更改为另一个值,然后在firebase上对其进行更新,但是当我对第一个元素执行此操作时,它也会同时更改列表的最后一个元素。 So, I need to reload the page to get it right again.
因此,我需要重新加载页面才能使其正确。
Someone has any clue of what is causing it? 有人有什么原因的线索吗?
i have same problem too. 我也有同样的问题。 But i think this can help you It look so stupid but it work for me GL
updatePost: function(post) { this.$firebaseRefs.posts.child(post['.key']).child('title').set(post.title) this.$firebaseRefs.posts.child(post['.key']).child('content').set(post. content) this.$firebaseRefs.posts.child(post['.key']).child('story').set(post. story) },
但是我认为这可以为您提供帮助。它看起来很愚蠢,但对我
updatePost: function(post) { this.$firebaseRefs.posts.child(post['.key']).child('title').set(post.title) this.$firebaseRefs.posts.child(post['.key']).child('content').set(post. content) this.$firebaseRefs.posts.child(post['.key']).child('story').set(post. story) },
You must use copy of the post 您必须使用帖子的副本
editPost: function(post) {
// Set post values to form
// create a copy of the post
this.newPost = {...post}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.