MatkoMilic
MatkoMilic

Reputation: 55

React Native FirebaseError: Function Query.startAfter() requires a valid first argument, but it was undefined

So Im receiving an error or:

FirebaseError: Function Query.startAfter() requires a valid first argument, but it was undefined.

My code:

getMore = async () => {
    let check = {};
    check = this.state.lastDoc;

    console.log(check); //returns a big object I will post below.
    if (check) {
      this.setState({ isMoreLoading: true });

      setTimeout(async () => {
        let snapshot = await firebase
          .firestore()
          .collection("posts")
          .orderBy("timestamp", "desc")
          .startAfter(check.data().id)
          .limit(2)
          .get();
  };

So I guess this error is about firebase.

WHAT console LOG (check) RETURNS JUST BEFORE BEING USED FOR startAfter: ITS CUT A BIT BECAUSE IT COULDNT BE POSTED: ITS CUT A BIT BECAUSE IT COULDNT BE POSTED:

 QueryDocumentSnapshot {
      "_document": Document {
        "converter": [Function anonymous],
        "
            },
            "name": Object {
              "stringValue": "NoviMatko",
            },
            "text": Object {
              "stringValue": "For wh perceive laughing six did far.",
            },
            "timestamp": Object {
              "integerValue": "1604771339487",
            },
            "uid": Object {
              "stringValue": "S2BxNARp4Cfinttb1TK5mzAHPb13",
            },
          },
          "name": "projects/eleph-6fee9/databases/(default)/documents/posts/djoSXIPRn1RMxMq2TD4C",
          "updateTime": "2020-11-07T17:49:00.119930Z",
        },
        "version": SnapshotVersion {
          "timestamp": Timestamp {
            "nanoseconds": 119930000,
            "seconds": 1604771340,
          },
        },
      },
    
          "
              
                  "[DEFAULT]" => Object {
                    "apiKey": "AIzaSyDwUBce3WYEl78xA6aag5nzb1xh2xC8dZQ",
                    
                      "lastLoginAt": "1604691856267",
                      "phoneNumber": null,
                      "photoURL": null,
                      "providerData": Array [
                        Object {
                          "displayName": null,
                          "email": "[email protected]",
                          "phoneNumber": null,
                          "photoURL": null,
                          "providerId": "password",
                          "uid": "[email protected]",
                        },
                      ],
                      "redirectEventId": null,
                    
                      },
                      "tenantId": null,
                      "uid": "S2BxNARp4Cfinttb1TK5mzAHPb13",
                    },
                  },
                },
                "instancesDeferred": Map {},
                "name": "auth",
              },
              
                },
                "container": [Ci...(truncated to the first 10000 characters)
    Got all results
    vVbq2mwrDMseBxCPDj15 => {"avatar":null,"name":"NoviMatko","text":"evoooo","timestamp":1605303464278,"uid":"S2BxNARp4Cfinttb1TK5mzAHPb13"}
    vG9p4xL8UqlZBkls1Xv1 => {"avatar":null,"name":"NoviMatko","text":"gdgdgdg","timestamp":1605303411975,"uid":"S2BxNARp4Cfinttb1TK5mzAHPb13"}
    xyyZQs5q9OLP2BdqDwJO => {"avatar":null,"name":"NoviMatko","text":"jkj","timestamp":1605136211563,"uid":"S2BxNARp4Cfinttb1TK5mzAHPb13"}
    jttcmEM8WFHOXxRIO9q9 => {"avatar":null,"name":"NoviMatko","text":"adawdadjnajodhjadjonadjjajdhajdnjadnjandjnadnadajdnjas","timestamp":1605116149227,"uid":"S2BxNARp4Cfinttb1TK5mzAHPb13"}
    qFJwWiS0dpaKFbVOWrG1 => {"avatar":null,"name":"NoviMatko","text":"gsgsefgsfsf😘😘😊😊","timestamp":1605115920543,"uid":"S2BxNARp4Cfinttb1TK5mzAHPb13"}
    qD8rrsexA4HrWHvCtfxR => {"avatar":null,"name":"NoviMatko","text":"JLKDBWJENKEFJOFBNKOJSBENFJKSNFJKLSNFJKSJNFJSKFNJSNFNSFNSNFNSJKLEFNSENFSNFJNSEJFNSJFNSJFNSEFNLSNF;SNFS;FH;SFJSFNSJFNSJNFJSNFJSNFNSNFNSFNSJFNSFNSF","timestamp":1605115869284,"uid":"S2BxNARp4Cfinttb1TK5mzAHPb13"}
    djoSXIPRn1RMxMq2TD4C => {"avatar":null,"name":"NoviMatko","text":"For who thoroughly her boy estimating conviction. Reme admire in giving. See resolved goodness felicity shy civility domestic had 
    but. Drawings offended yet answered jennings perceive laughing six did far.","timestamp":1604771339487,"uid":"S2BxNARp4Cfinttb1TK5mzAHPb13"}
    eILcb0rPOZ0Rpeoym6uX => {"avatar":"https://firebadia&token=272cfbff-3fd7-4216-a6c3-09ed612eca3e","text":"Fjjfjffj","timestamp":1604395119914,"uid":"oTPw7q2jRWcE3F92U4aXeHHTHDg1"}
    y7FN13SKWUDsWdKsGJiT => {"commentCount":3,"likeCount":5,"mood":"Yy","text":"Today is such 
    a joyful day because its Monday and at Mondays we dont do much at my work #blessed","timestamp":1592776918943,"userHandle":" NRQ2Ewhx76cWVqntRPLV1ygYHB73"}

Upvotes: 0

Views: 528

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

The error message is telling you that check.data().id was undefined, not check. If you want to see the value of check.data().id, you should log exactly that.

What's likely happening here is that the document you have in hand here doesn't have a property called "id". That would explain why you're seeing this error message.

If you want to use startAfter() correctly, you should actually just pass the entire DocumentSnapshot object to it:

        let snapshot = await firebase
          .firestore()
          .collection("posts")
          .orderBy("timestamp", "desc")
          .startAfter(check)
          .limit(2)
          .get();

For more information, see the documentation on the pagination APIs.

Upvotes: 1

Related Questions