Building connections in parse.com using various classes (JavaScript SDK)

Currently using parse.com in conjunction with the JavaScript SDK.

Despite my extensive research, I have been unable to find any examples that directly address the issue I am facing.

Here is the specific goal I am striving to accomplish:

I aim to establish a one-to-many relationship between the "FriendRequest" class and the "myBadges" class. The child objects within "myBadges" will gradually increase over time.

These child objects are generated by a function that triggers when a user selects a badge and clicks "Yes, do it now!" to add it to their collection in "myBadges".

Presently, I am only able to create a relationship within the active class used by the function. As a result, the relation set up in "myBadges" merely points to the User class instead of the "FriendRequest" class.

The code snippet responsible for this relationship is as follows:

success: function(results) {
    // Object successfully saved.
    userbadges.relation('BadgeConnect').add(userbadges);
    userbadges.save();

Hence, my dilemma revolves around transferring the link from the "myBadges" class to the "FriendRequest" class. Any illustrative example demonstrating how to achieve this would greatly aid me in understanding the correct methodology.

COMPLETE CODE BELOW.

<script type="text/javascript>
    Parse.initialize("xxxx", "xxxxx");
    var MyBadges = Parse.Object.extend("myBadges");
    var friendRequest = Parse.Object.extend("FriendRequest");

    var userbadges = new MyBadges();
    var friendRequest = new friendRequest();
    var user = Parse.User.current();

    $(document).ready(function() {

        $("#send").click(function() {
            var badgeselected = $('#badgeselect .go').attr("src");
            userbadges.set("BadgeName", badgeselected);
            userbadges.set("uploadedBy", user);

            userbadges.save(null, {
                success: function(results) {
                    // Object saved successfully.
                    userbadges.relation('BadgeConnect').add(userbadges);
                    userbadges.save();
                    //location.reload();
                },
                error: function(contact, error) {
                    // Save failed.
                    alert("Error: " + error.code + " " + error.message);
                }
            });
        });
    });

Answer №1

When faced with a problem, there are always multiple solutions available.

If you aim to query this relationship from both sides, there are two primary approaches:

  • Implement a Cloud Function for after-saving your myBadges class to replicate the relation on the FriendRequest side. The feasibility of this method is uncertain as I have not personally tested it.
  • Develop your own relationship class, such as myBadges_FriendRequest, containing references to each respective class along with any other relevant information about the connection.

In my opinion, I would recommend the second option. By utilizing this method, you can easily retrieve data based on either end of the relationship and utilize the include() function to populate both pointers in the output when necessary.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript: Obtaining a Distinct Identifier for Various Replicated Entries

Imagine we have an object: var db = [ {Id: "201" , Player: "Jon",price: "3.99", loc: "NJ" }, {Id: "202", Player: "Sam",price: "4.22", loc: "PA" }, {Id: "203" ,Player: "Sam",price: "4.22", loc: "NY" }, {Id: "204", Player: ...

Hiding and showing div elements using CSS, JavaScript, and PHP

Here is the current code snippet: <? while ($row1 = mysql_fetch_object($result1)) { echo '<a href="#" onclick="showhide("'.$row1->id.'");">Name</a>'; while ($row2 = mysql_fetch_object($result2)) { ...

Whenever I try to import a directory that contains modules, Webpack encounters an error

I am currently in the process of developing a small npm library to streamline API interaction. Here is an overview of my folder structure... dist/ index.js src/ index.js endpoints/ endpoint1.js package.json webpack.config.js Inside my src/index ...

Enable seamless SCSS inclusion in Vue components through automatic importing

My goal is to import a variables.scss file globally in my Vue project. I have set up my vue.config.js file as follows: module.exports = { css: { loaderOptions: { scss: { additionalData: `@import "@/st ...

Jquery's mouseclick event selects an excessive number of elements

Is there a way to retrieve the ID of an element when it is clicked, only if it has one? I currently have this code set up to alert me with the element's ID: $("[id]").click(function(event) { event.preventDefault(); var id_name = $(this).attr ...

What is the best method for creating a loop script within a widget using script?

I am trying to implement a loop within a widget, however, the widget contains an internal script: <!DOCTYPE html> <html><head></head><body> <script> list=["AMAR3","BBDC4", "BEEF3", "BPAN4", "BRFS3", "B3SA3", "CVCB3" ...

How can you fetch data from a PHP file using AJAX before performing a header redirect?

I am currently in the process of adding more dynamism to my website. I have developed a forum from scratch and now I am integrating JavaScript into it. All the PHP backend work is complete. My next goal is to enable user login without having to refresh the ...

Waiting for an Element to Become Visible in Selenium-Webdriver Using Javascript

When using selenium-webdriver (api docs here), how can you ensure that an element is visible before proceeding? Within a set of custom testing helpers, there are two functions provided. The first function successfully waits for an element to exist, howeve ...

MongoJS Node findOne query inaccurately returns empty result set

Utilizing MongoJS: https://github.com/mafintosh/mongojs Retrieves all data Returns an empty array

The database contains the data I am looking for. I've also tried searching using a different key (such as name). Why is it unable to locate the da ...

Setting the dimensions of an HTML - CSS block

I am trying to style a navigation bar using the following CSS code: #nav {} #nav a { position: relative; display: inline-block; color: #F0F0F0; width: 1em; height: 2em; line-height: 0.9em; } #nav a.ic ...

Unlocking the potential of GraphQL: Harnessing the power of sibling resolvers to access output from another

Could use a little assistance. Let's say I'm trying to retrieve the following data: { parent { obj1 { value1 } obj2 { value2 } } } Now, I need the result of value2 in the value1 resolver for calculation ...

What is the best way to safely distribute the same npm package with multiple contributors?

Imagine a collaborative open source project with multiple contributors working on it. As the project progresses, these contributors need to publish their work to the NPM registry. But how can this be done securely when multiple people are involved? The ow ...

"Failed authentication for client" in the testing environment of PayPal sandbox

I obtained the code from github and updated the Client ID & App Secret accordingly. It worked flawlessly. https://github.com/paypal-examples/docs-examples/tree/main/advanced-integration However, upon integrating the codes into my project, I encountered th ...

What is the best way to send an HTML form variable to a Perl script using AJAX?

My goal is to pass the HTML variable from the list menu to the Perl script using POST. However, when I try to do this, the jQuery ajax() function executes the Perl script without including the value obtained from document.getElementById. Below is the sni ...

What is the best way to compare all the elements in an array to the entries in another object, while also storing the results of each comparison?

I am working with a JSON file that contains an array of 2 objects: `{ bg: 'a', o: 'c' }`, `{hg': 'a2', 'oo': 'c3'}`. My goal is to compare each object in the array with another object structured as fol ...

Material-UI Scroll Dialog that begins scrolling from the bottom

While attempting to incorporate a scrolling div with the ref tag inside Dialog Material-UI Design, I encountered an error stating Cannot read property 'scrollHeight' of undefined When running my code outside of the Dialog, it functions correctly ...

Exploring the significance of a super in Object-Oriented Programming within JavaScript

During my studies of OOP in JS, I encountered the super() method which serves to call the constructor of the parent class. It made me ponder - why is it necessary to invoke the parent class constructor? What significance does it hold for us? ...

Unable to submit form in Node.js with Express

I am just starting out with node.js and I need help with a sample form to insert values into a database. Below is the code snippet for my test page: <form action="/create" method="POST" class="form-horizontal" enctype="application/x-www-form-urlencode ...

AngularJS - Choose and establish initial values for Editing or Creating New Items

My first project involving AngularJS has left me a bit stuck when it comes to using the select list. I need to either set the default value to the first option for a new entry, or if it's an edit, select the appropriate value. In my form, there are t ...

Is it possible to modify the inline onkeyup function?

Looking for a solution to change the onkeyup function in a textarea element from 255 characters to 150 characters without directly editing the code? <textarea cols="50" rows="4" id="textbox" onkeyup="limitArea(this, 255, '')"></textarea ...