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 ?

Alperen ARICI
  • 204
  • 6
  • 13
  • 2
    Why are you using a callback at all when you have a promise-based `async` function? Just `return` the result. – Bergi Apr 04 '20 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`. – Bergi Apr 04 '20 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)` – MwamiTovi Apr 04 '20 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) – Cam Song Apr 04 '20 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 }) })}` – Alperen ARICI Apr 04 '20 at 16:50
  • Alright, let me explain the concept in a bit in the answer section for those who might miss this comment. – MwamiTovi Apr 04 '20 at 17:02

1 Answers1

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.

MwamiTovi
  • 2,425
  • 17
  • 25