I'm currently working on a small project thats supposed to teach me how to properly implement databases. It is supposed to be a small userlist with "currency" and items. Since I'm still a beginner I wasn't too confident in creating everything from scratch, so I am currently using React, MongoDB and Stitch.
When I'm working on the app on my development server, everything works fine. It connects and displays the proper data without any kind of error. But once I tried hosting it online, I got an error: Error Message: "MustAuthenticateFirst".
Code
export const Bank = () => {
const [user, setUser] = useState([]);
function initializeClient() {
const client = Stitch.initializeDefaultAppClient("xxxx");
const mongodb = client.getServiceClient(
RemoteMongoClient.factory,
"mongodb-atlas"
);
const db = mongodb.db("nnbank");
displayBankOnLoad(client, db);
}
function displayBankOnLoad(client, db) {
client.auth
.loginWithCredential(new AnonymousCredential())
.then(displayBank(db))
.catch(console.error);
}
function displayBank(db) {
db.collection("user")
.find()
.toArray()
.then(user => {
setUser(user);
})
.catch(console.error);
}
useEffect(() => initializeClient(),[]);
I can't seem to find the reason for this error and I couldn't find any solution online yet. So if you guys have any idea, I really appreciate it.