Unable to execute multiple queries within a CloudCode query

In my quest to discover new users who I have not connected with before using a cloud function, I am utilizing several tables:

  1. Users table - This standard table includes an additional "hasLight" boolean column
  2. Connected table:
    • 'user' - a pointer to the user (first side)
    • 'userId' - objectId of the user
    • 'user2' - a pointer to the user we are connecting with
    • 'userId2' - objectId of the user we connected to
  3. Available table:
    • 'userObjectId' - objectId of the user
    • 'counter' - number

The code snippet:

Parse.Cloud.define("findFreshUser", function(request, response) {

    Parse.Cloud.useMasterKey(); // For reading all the tables //

    var user = Parse.User.current();

    // Retrieve all potential users we have previously connected with //
    var connectQuery = new Parse.Query("Connected");
    connectQuery.include('userId2');
    connectQuery.equalTo("userId", Parse.User.current().id);

    // Find all users with an availability counter of '0' //
    var availableQuery = new Parse.Query("Available");
    availableQuery.notEqualTo("counter", 0);  

    var freshUserQuery = new Parse.Query(Parse.User);

    freshUserQuery.doesNotMatchKeyInQuery("objectId", "userId2", connectQuery); // To find users we haven't connected with before - THIS PART ISN'T WORKING!!! //
    freshUserQuery.doesNotMatchKeyInQuery("objectId", "userObjectId", availableQuery); // Exclude users with '0' availability - THIS PART IS WORKING //

    freshUserQuery.equalTo("hasLight", false); // Users must not have a light //
    freshUserQuery.descending("updatedAt");
    freshUserQuery.limit(1); // We only need one user //

    freshUserQuery.find({ 
        success: function(results) {
            if (results.length>0){
                console.log("Found the user "+ results[0].id);
                response.success(results[0].id);
            }else{
                response.error("No user found");
            }

        },
        error: function() {
            response.error("No user found");
        }
    });

});

For some reason, CloudCode is ignoring the connectQuery when both doesNotMatchKeyInQuery statements are used together:

If I use only

freshUserQuery.doesNotMatchKeyInQuery("objectId", "userId2", connectQuery);

and comment out

freshUserQuery.doesNotMatchKeyInQuery("objectId", "userObjectId", availableQuery);

it works. It seems like there may be a conflict between using both at the same time. How can I make them both apply?

This issue appears to be related to Parse, but as someone new to CloudCode, I may not be implementing it correctly.

Note: It's worth noting that instead of comparing the user object itself, I am comparing their IDs as part of troubleshooting. I understand there are ways to improve the code and database design for better efficiency.

Answer №1

To accomplish this type of query, it is necessary to utilize Promises: blog.parse.com/learn/engineering/whats-so-great-about-javascript-promises/

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

Running a jQuery GET request to execute a script

When utilizing jQuery, I am making a GET request. My request includes adding '?update' to the URL: $.get("/index.html", "update", function (data) {/* Insert code here */}, "html"); The 'data' variable stores the response from my serve ...

Is there a way to implement word wrapping in li text without making any changes to its width

Is there a method to wrap the content inside li elements without modifying their width? As an illustration, consider the following structure - <ul> <li>Text Example</li> <li>Text Example</li> <li>Text Exampl ...

Error encountered in React due to a syntax issue with parsing JSON from the response received

The structure of my node.js express server is as follows: const express = require('express'); const app = express(); const port = process.env.PORT || 5000; app.listen(port, () => console.log(`Listening on port ${port}`)); app.get('/exp ...

Utilize JavaScript to randomly choose images as background tiles in HTML

Currently, I am in the process of developing a game using HTML/CSS/JavaScript. My background is currently set to a single image (100px / 100px) being repeated vertically and horizontally to tile across the entire page body; CSS: body { background-ima ...

Ways to parse a json document in vue.js

As a novice in web development, I am currently working with vue-cli3.0 and a local server. <template> <div> </div> </template> <script lang="ts"> import { Component, Prop, Vue } from 'vue-property-decorator&apos ...

Enhancing the functionality of XMLHttpRequest.open()

How can I intercept and modify the arguments of the XMLHttpRequest.open() method? I attempted using the proxy method, but it was unsuccessful. I removed the override when XMLHttpRequest() was called: (function() { var intercepted = window.XMLHttpReque ...

Express encountered an issue when trying to upload a file through AngularJS

I am currently facing an issue with uploading a file to express and subsequently to mongoDB. Despite my efforts, when I attempt to upload the file to express, it appears that no data is being passed through the response. I am utilizing ng-file-upload.min. ...

Utilizing jQuery and AJAX for submitting multiple POST requests

Experiencing a problem with posting data via AJAX using a drag and drop interface. The data is being sent to the POST URL as intended, but there's a recurring issue where the POST request occurs twice, causing the processing URL to handle the data aga ...

Learn how to dynamically toggle the visibility of tabs in CodeMirror with a simple button click

Currently, I am developing a Python editor for a website using the Code Mirror JavaScript library. There is a button called toggle invisibles that, when clicked, displays spaces and end-of-line markers. I also wanted to show tabs, which are not supported b ...

Unable to use jQuery to update a link address

I am currently in the process of developing a Google Analytics plugin and encountering an issue with pagination that relies on Ajax. Each next and previous link triggers a request for data since it serves as the mechanism for pagination. (Problem resolved; ...

What is the best way to manage the "checked" state of an input checkbox using React?

I'm currently developing an application that features a form with radio buttons. One of the radio button options can be toggled on/off using a checkbox, but I want to ensure that the checkbox is disabled if the corresponding radio button is not selec ...

Error in NodeJS session: the variable "request" is not defined

Recently, I have started working with NodeJS and I am currently facing an issue while trying to implement sessions in my project. Any assistance or guidance on this matter would be greatly appreciated. Below is the code snippet where the error occurs: va ...

Displayed in the xmlhttp.responseText are the tags

Why do tags appear when I input xmlhttp.responseText into my textbox? It displays <!DOCTYPE html><html><body></body></html> along with the desired content. Is there a way to prevent the tags from being displayed? Here is the ...

Creating an interactive button using RichFaces and Bootstrap for seamless Ajax functionality

I have a challenge with making a fully clickable span or button that includes a Bootstrap refresh glyph-icon. The current code I have inherited only allows the icon itself to be clicked, leaving the area between the icon and button border unclickable. This ...

Iterate through an array of objects, applying a filter and simultaneously generating a new object

I have an array of elements structured like the example below. In this structure, there are nested rows within input, and each rows object contains an array of objects. let input = [ { "title": "ENGINEERING", "rows": [ { "ri ...

Inhibit the ASP:dropdownlist when the value of another ASP:Dropdownlist is selected

Trying to modify two asp.net dropdownlists using client-side JavaScript is my current challenge. Specifically, these dropdowns are located within a modal that opens with a function. The objective is to disable the second dropdown (dropdown2) whenever the ...

Within Codesandbox using Vue, the error message 'The property or method "children" is not defined in the instance, but is referenced during rendering.' appeared

Currently, I am in the process of creating a versatile State Manager that can seamlessly integrate with various Frontend Frameworks, including Vue. In order to showcase how the State Manager can be utilized within Vue, I have set up a simple codesandbox de ...

Utilizing the toggle switch functionality in Django with a boolean field implementation

My work_experience model includes an "is_working" field which is set to true when a user is currently employed at a company. I am using a toggle switch on the front end and would like to update the boolean field value for "is_working" with a click. What lo ...

How to successfully implement an AJAX GET request in Express.js

Currently, I am utilizing node.js and Express.js on the backend and attempting to initiate a server call from the client using AJAX. In my implementation, the following POST request is functioning properly with AJAX: node.js/Express.js: app.post('/ ...

Vue component encounters issue with exporting JavaScript functions

I decided to streamline my code by moving some functions into a JavaScript file like this: [...] function changeToEditView(reportId) { let pathEdit="/edit/"+reportId; this.$router.push({ path: pathEdit, replace: true }); } [...] export {c ...