How to manipulate a Mongodb-Database with Construct 3 using the Realm Web SDK?

0 favourites
  • 12 posts
From the Asset Store
Casino? money? who knows? but the target is the same!
  • Hello there!

    I am having trouble setting the base to access the Mongodb database with the Construct 3 Client App via the Mongodb Realm Web SDK.

    What I have so far:

    But when I then try to execute

    const customData = app.currentUser.customData
    console.log(JSON.stringify(customData, null, 4));
    

    I will just see {} in the console and not the users customData.

    When I have the basis I'm able to edit and adabt it to my needs. But completely figuring this out on my own troubles me since weeks now. The documentation on Mongodb, although being detailed, is very confusing and the examples they list are very lackluster and scattered across 4 different documentation sites.

    Please, may someone with better coding capabilities be so generous and provide me with an example on how to read, write and edit/update database data from within a Construct 3 clients app?

    I'm looking forward to your replies. <3

  • What have you tried doing to set the custom data?

    I am not very familiar with the library, but if it is returning an empty object, then it is likely nothing was set to begin with.

  • Hello and thanks for the reply!

    For now I just manually inserted data into the custom_data object inside the UserAccounts database.

    And now I'm trying to read it. When I have this done the next step would be setting a new value or modify existing data.

  • Is this how you are setting the custom data?

    docs.mongodb.com/realm/web/access-custom-user-data

    I would try doing it that way, and then see if retrieving it works. There is a note about write permissions, which I am not too sure what it means, but it might be worth to keep it in mind if you can't write the data.

  • It seems you need to enable custom user data before you are able to use it.

    docs.mongodb.com/realm/users/enable-custom-user-data

  • Hi thanks again for your replies!

    I enabled custom user data before. And for how I set it I manually set the data into the database using Mongodb Atlas.

    I wanted to try the easier way first. But you are right. I can try setting custom user data and see what will happen.

  • Just one more thing I noticed reading the documentation.

    docs.mongodb.com/realm/web/access-custom-user-data

    There is a warning about stale data.

    So you might want to try doing

    await app.currentUser.refreshCustomData();
    

    before getting the custom data to make sure you are getting the latest available.

  • Thank you for pointing that out.

    I tried it again and it still doesn't show the data, although the data is only stale for about 30 minutes and now many hours passed since I updated it manually.

    Thats how it currently looks like:

    And this is what I get in the console:

    Note that the most errors come from the official facebook plugin. I still don't know why because facebook login works just fine.

    Signing in to Realm works too. One line above the blue marked area it says signed in and it also shows the Realm user id. And when a new user signes into facebook a new user account will be created in the Mongodb database through Realm.

  • I am just guessing here, but judging from the console errors, there seem to be syntax error in either here

    const mongodb = app.services.mongodb("mongode-atlas");
    

    In this one, I don't think app has any property called services. Maybe you need to do something like this

    const mongodb = app.currentUser.mongoClient("mongode-atlas");
    

    Haven't actually tried this myself, just stitching together things I find that look like they make sense.

    The next line

    const collection = mongo.db("ServerDB").collection("UserAccounts");
    

    I think should be

    const collection = mongodb.db("ServerDB").collection("UserAccounts");
    

    Maybe that won't blow up, but it is still not doing anything useful, just getting some objects.

    Next you can try with

    collection.updateOne({ $set: { myNewProperty: "foo" } })
    .then(() =>
    {
     // Refresh the user's local customData property
     return app.currentUser.refreshCustomData()
    })
    .then(() =>
    {
     // try to console.log the custom data here to see if something changed
    });
    
    

    After doing this also check the database, to see if that was updated.

    I don't think I can help anymore without actually working on this :P

  • Awesome! With your help I was able to think again, read through the documentation again and find a mistake that I did before my holidays. After the holidays I forgot about my changes.

    I chose another database for custom data back then. Because I wanted to have account data and custom data on separate databases for security reasons. Now that I changed it to the default path I was able to read a users custom data with:

    const customData = app.currentUser.customData
    console.log(customData);
    

    And generally read documents:

    const mongodb = app.currentUser.mongoClient("mongodb-atlas");
    const PrivateUserData = mongodb.db("ClientDB").collection("PrivateUserData");
    const myID = app.currentUser.id
    const myData = await PrivateUserData.findOne({ id: myID });
    console.log("My Data: ", myData);

    And to update a document:

    const result = await PrivateUserData.updateOne(
     { id: myID },
     { $set: { dong: "Very schlong" } }
    );
    console.log(result);

    And to update custom user data should be:

    const mongodba = app.services.mongodb("mongodb-atlas");
    const collection = mongodba.db("ServerDB").collection("UserAccounts");
    await collection.updateOne(
     { userID: app.currentUser.id },
     { $set: { dong: "12cm" } }
    );
    

    But I didn't test the last one yet.

  • Cool, well done!

    Before I forget, yesterday I found the full reference for the library, but it's a bit hidden away. It's very useful when you go past the basic examples.

    docs.mongodb.com/realm-sdks/js/latest

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Ah thank you! That will be very useful, bookmarked.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)