How to use the Parse Server JS SDK with vanilla Javascript
I couldn’t find any documentation regarading how to use the Parse Server JS SDK in Javascript project, so here is how I did it:
- First, include the pre-compiled SDK in your html file, you can fetch it from npmcdn :
<-- index.html --> <script src="https://npmcdn.com/parse/dist/parse.min.js"></script>$
I use the minified version in this case.
- Then, just use the Parse() object in your Javascript code:
Parse.serverURL = 'http://localhost:1337/parse' Parse.initialize("yourappid", "YOUR_JAVASCRIPT_KEY") var Gamer = Parse.Object.extend("Gamer"); var query = new Parse.Query(Gamer); query.find().then((results) => { // The object was retrieved successfully. var arrayLength = results.length; for (var i = 0; i < arrayLength; i++) { let player = results[i]; console.log(player.attributes); } }, (error) => { // The object was not retrieved successfully. // error is a Parse.Error with an error code and message. console.error(error); }); var player = new Gamer(); player.set("score", 1337); player.set("playerName", "Sean Plott"); player.set("cheatMode", false); player.set("skills", ["pwnage", "flying"]); player.save()
That’s it!