我在从VueFire更新Firebase时遇到了问题。我正在尝试使用以下方法,但是如果我将任何字段留为空白(在安装程序中应该经常发生),它就会对我大喊大叫--如果.update中有空白字段,它为什么会生气呢?
错误:未定义错误: Firebase.update失败:第一个参数包含属性'businesses.somebusiness.video‘中未定义的参数
updatePost(post) {
postsRef.child(post['.key']).update({
name: post.name,
video: post.video,
story: post.story,
cover: post.cover,
card: post.card
})
},有一次,我把上面的内容改写成这样:
updatePost: function (post) {
const businesschildKey = post['.key'];
delete post['.key'];
/* Set the updated post value */
this.$firebaseRefs.posts.child(businesschildKey).set(post)
},令人惊讶的是,删除密钥似乎在Vue中引起了奇怪的排序问题。如果我能找到一种方法来避免出现错误,如果一个方法是空的,我宁愿坚持使用top方法。
发布于 2017-04-14 20:45:33
据this post称,
当将对象传递给Firebase时,属性的值可以是值或
null(在这种情况下,属性将被删除)。它们不可能是undefined,这就是根据错误传入的内容。
您的错误消息表明post.video的值是undefined。您可以使用逻辑-或者提供类似于这样的回退值:
video: post.video || null,这意味着每当post.video具有false-y值时,表达式将计算为null。但是,它可以捕获空字符串或数字0。要更准确地说,您应该使用
video: typeof post.video === 'undefined' ? null : post.video,如果您需要对许多值执行此检查,则可以为其编写一个函数:
function nullIfUndefined(value) {
return typeof value === 'undefined' ? null : value;
}那你的表情就是
video: nullIfUndefined(post.video),https://stackoverflow.com/questions/43416461
复制相似问题