2

I have a function, that is returning callback. But meetUid is static on here.

fetchMeetDetails = async (callback) => {

        const meetUid = '1a712f91-974d-4185-9389-f7b1b4edede2';
        const snapshot = await database().ref(`/meets/${meetUid}`).once('value');

        callback(snapshot.val())

    }

I want to get meetUid from parameters. like that fetchMeetDetails = async (callback,meetUid) => { but I cannot do it. Because we got an error (TypeError : callback is not a function). How can I use this function with callback and parameters ?

6
  • 2
    Why are you using a callback at all when you have a promise-based async function? Just return the result. Commented Apr 4, 2020 at 16:42
  • Adding a parameter for it should work just like that. Please show us the code that produces this type error, including the part where you are calling fetchMeetDetails. Commented Apr 4, 2020 at 16:43
  • 1
    That's interesting. The way you pass callback as a parameter is unclear thus the compiler complains. Try this (callback=f=>f, meetUid) Commented Apr 4, 2020 at 16:45
  • It is pretty obvious, isn't it? Can you provide the snippet where you call the fetchMeetDetails function? You need to pass a javascript function to fetchMeetDetails in order to execute something like callback(foo). Callback is just a name convention for javascript function which will be excuted after an async action returns a result (success or error) Commented Apr 4, 2020 at 16:48
  • @Bergi My reason for using callback, where componentdidmount is not waiting for a response. Blank data is printed on the screen. So I use callback. And I call this function on here => ` componentDidMount() { FirebaseDB.fetchMeetDetails((result) => { this.setState({ meet: result }) })}` Commented Apr 4, 2020 at 16:50

1 Answer 1

1

TypeError : callback is not a function
Type is the key word here.

Compiler takes the callback parameter as anything because of how you declared it first.
Not a problem.

Next, compiler meets this callback(snapshot.val()) and then it gets confused.
Then it says, "Well, am confused with this type. Let me complain!"

In such situation, the compiler wants to know the default type of that parameter.

// Since here "callback" is a function, pass "callback" as a default function

(callback=f=>f, ...rest) => {}

It's the same concept when passing "props" to "children" in react.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.