score:1

Accepted answer

i think adding a return into your getprojects() service will solve your issue.

import {
  stitch,
  remotemongoclient,
  anonymouscredential
} from "mongodb-stitch-browser-sdk";

export function getprojects() { //add async if you need to await in func body
  const client = stitch.initializedefaultappclient("------");
  const db = client
    .getserviceclient(remotemongoclient.factory, "-----")
    .db("----------"); // if these above are async, then await them as well.

    // added return keyword here
    return client.auth // should return promise to await in your component
    .loginwithcredential(new anonymouscredential())
    .then(() =>
      db
        .collection("--------")
        .find({}, { sort: { project: 1 } })
        .asarray()
    )
    .then(res => {
      console.log("found docs", res);
      console.log("[mongodb stitch] connected to stitch");
      return res;
    })
    .catch(err => {
      console.error(err);
    });
}

edit 1:

for refactoring, i think pairing redux and redux-saga will give you very good separation of concern and a way to easily write test if you plan to do so. but overall, i think this tweak above can at least solve your issue.


Related Query

More Query from same tag