1
\$\begingroup\$

I was trying to implement MongoDB with my Unity WebGL and everything is working fine so that I managed to insert and retrieve documents from my mongo database with no issues, so everything was perfect so far until I compiled the project and realized that my WebGL build won't communicate with my database at all! so took a look at the console and I see this error:

MissingMethodException: Default constructor not found for type MongoDB.Bson.Serialization.BsonClassMap`1[[UserAccount, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]

I have no clue what's happening, tried compiling the project for window/linux and I got the same error, then I changed API compatibility level to 4.x and it was working fine on the windows build, but not on WebGL ( API compatibility level=4.x is checked for WebGL build!)

can someone lend me a hand with that?

\$\endgroup\$
1
\$\begingroup\$

What you are trying to do here unfortunately can not work. The MongoDB protocol is based directly on TCP/IP. But a web application running in a web browser can not create raw TCP/IP connections. There is no API in JavaScript for doing that. The closest it gets are HTTP requests ("AJAX") and WebSockets (which are intentionally incompatible with regular sockets). A WebGL build is a JavaScript application, so if the JavaScript API can't do something, then Unity won't magically allow you to do it.

If you want to use a MongoDB database from your Unity application running in the browser, then you will have to use a middleware which provides a REST API for MongoDB which you can then access from your game by using the UnityWebRequest class.

\$\endgroup\$
  • \$\begingroup\$ that explains everything, it seems I have plenty of work ahead :(, thank you a lot for your help! you saved me! \$\endgroup\$ – roseicollis Apr 10 at 13:19
  • \$\begingroup\$ @roseicollis Are you already committed to using MongoDB as your database? If you are looking for a document-oriented NoSQL database which offers a REST API out-of-the-box, then you might want to take a look at CouchDB. \$\endgroup\$ – Philipp Apr 10 at 13:29
  • \$\begingroup\$ I'll take a look at that, thanks for the suggestion! \$\endgroup\$ – roseicollis Apr 10 at 14:06
  • \$\begingroup\$ @roseicollis When this post answered your question, please accept it by clicking the checkmark-icon next to it. \$\endgroup\$ – Philipp Apr 11 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.