简体   繁体   中英

pushRef.set() is not a function, Firebase query

This is my code:

var pushRef = currentAssignment.child('answers').push().catch(e => console.log('push', e));
pushRef.set({
    // downloadURL: downURL,
    textAnswer: textAnswer,
    date: this.generateDate(),
    seen: false,
    // firebaseKey: pushRef.getKey(),
    workKey: this.props.questionId
})

When I try to run it, I get this error: pushRef.set() , but according to this part of the documentation it looks to me like I'm doing everything the same way. Here's Google's example of push instruction:

var postsRef = ref.child("posts");
var newPostRef = postsRef.push();
newPostRef
    .set({
        author: "gracehop",
        title: "Announcing COBOL, a New Programming Language"
    });

// we can also chain the two calls together
postsRef
    .push()
    .set({
        author: "alanisawesome",
        title: "The Turing Machine"
    });

So what am I missing?

By adding .catch(e => console.log('push', e)) behind push() you change it from a firebase database reference to something else. So removing that will fix this.

As far as I know calling push() (without parameters) will never generate an error, it simply generates a firebase reference (unique key) client side.

Your code is expecting that catch() returns a database reference. It doesn't - catch() always returns a promise.

The use of catch here is unnecessary because push() with no arguments is a completely local operation. It returns a database reference that can also be used like a promise (a ThenableReference ).

If there is any error to possibly catch here, it's on the promise returned by set() . That call will fail if security rules are violated during the write operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM