Auzy
Auzy

Reputation: 2155

Vuefire Firebase update issues

I'm having som issues with updating Firebase from VueFire. I m trying to use the following method, but it yells at me if I leave any field blank (which is supposed to happen often in setup) Any idea why this gets mad if .update with a blank field?

Error: Uncaught Error: Firebase.update failed: First argument contains undefined in property '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
        })
      },

At one point I had the above re-written like so:

updatePost: function (post) {
        const businesschildKey = post['.key'];
        delete post['.key'];
        /* Set the updated post value */
        this.$firebaseRefs.posts.child(businesschildKey).set(post)
      },

It worked amazingly but deleting the key seemed to cause weird ordering issues in Vue. I would prefer to stick with the top method if I can find a way to not have it trow an error if one is left blank.

Upvotes: 0

Views: 593

Answers (1)

Roy J
Roy J

Reputation: 43881

According to this post,

When you pass an object to Firebase, the values of the properties can be a value or null (in which case the property will be removed). They can not be undefined, which is what you're passing in according to the error.

Your error message suggests that post.video's value is undefined. You can use logical-or to provide a fallback value like so:

  video: post.video || null,

That means whenever post.video has a false-y value, the expression will evaluate to null. That could catch empty string or numeric 0, though. To be more precisely correct, you should use

  video: typeof post.video === 'undefined' ? null : post.video,

If you need to do this check for many values, you can write a function for it:

function nullIfUndefined(value) {
  return typeof value === 'undefined' ? null : value;
}

then your expression would just be

  video: nullIfUndefined(post.video),

Upvotes: 2

Related Questions