Utilize computed fields in Parse Object queries

Currently, I am working on developing a Parse Server API for a cooking-centered social network. One of the key features I am focusing on is creating a function that retrieves recipes. I want this function to be able to display calculated fields such as the title field based on different languages (titleFr, titleEn, titleEs, etc.). I have come across suggestions to use Parse.Promise but I am having trouble implementing it successfully. Despite my efforts, the function only exposes stored fields. Can anyone provide guidance on how to achieve this?

Parse.Cloud.define('getRecipes', function(request, response) {
    var lang = getLocale(request); // a custom-made function 
    var recipes = new Parse.Query("Recipe");
    recipes.find().then(function (recipes) {
        console.log("Successfully retrieved " + recipes.length + " recipes.");
        var recipe = recipes.map(function (recipe) {
            recipe.title = recipe.get("title" + lang);
            return recipe;
        });
        return Parse.Promise.when(recipe);
    }).then(function (results) {
        response.success(results);
    }).fail(function(error) {
        alert("Error: " + error.code + " " + error.message);
    });
});

Answer №1

Consider using return recipes.map.... in place of

var recipe... return Parse.Promise.When(recipe);

It seems like the variable recipe is not a promise, so it may not be handled in the same way as expected here. However, this could be incorrect.

Additionally, please note that there should be a response.error() call included if the query fails.

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

An error occurs in TypeScript when attempting to reduce a loop on an array

My array consists of objects structured like this type AnyType = { name: 'A' | 'B' | 'C'; isAny:boolean; }; const myArray :AnyType[] =[ {name:'A',isAny:true}, {name:'B',isAny:false}, ] I am trying ...

Transforming the API response

After making an Ajax call, the response received is: console.log(data); {"valid":true,"when":"Today"} Attempting to read the response like this: var res = data.valid; console.log(res); results in 'undefined' being displayed. To address this i ...

Learn how to successfully carry on with event.preventdefault in JavaScript

Is there a way to create a modal that prompts the user to confirm if they want to leave the page without committing changes? These changes are not made within a <form> element, but rather on a specific object. I've attempted to use both $route ...

Delivering JSON and HTML safely to JavaScript applications

As I brainstorm secure methods for delivering HTML and JSON to JavaScript, my current approach involves outputting JSON in this manner: ajax.php?type=article&id=15 { "name": "something", "content": "some content" } However, I am aware of the ...

Error 404 in Laravel: Troubleshooting AJAX, JavaScript, and PHP issues

I have a dynamic <select> that depends on the value selected in another static <select>. However, I am encountering an issue: http://localhost/ajax-model?make_id=8 404 (Not Found). This problem occurs on the page where both select elements are ...

React is throwing an error message stating that setCount is not a valid function

Getting an error saying setCount is not a function. I am new to this, please help. import React, { memo, useState } from "react"; export const Container = memo(function Container() { const { count, setCount } = useState(0); return ( ...

Input information into the system from the successful response found on a different page

After receiving my ajax response successfully, I want to redirect to a new aspx page where there are 30 input type text controls. My goal is to populate all the values in those controls. How can I achieve this? Any suggestions? This is what I have attempt ...

How to retrieve an object within an object using a dynamic key in Vue JS?

Within my Vue JS 2.x project, I am utilizing a v-for loop to iterate over an array of objects. Each object in the array contains a dynamically named "key", which is unique to each object. My goal is to access the data associated with each key in order to d ...

Can we enhance this JavaScript setup while still embracing the KISS principle (Keep it simple, Stupid)?

I have completed the following tasks: Ensured all event handlers are placed inside jQuery DOM ready to ensure that all events will only run Defined a var selector at the top of events for caching purposes Please refer to the comments below for what I be ...

steps for including a JS file into Next.js pages

Is it possible to directly import the bootstrap.bundle.js file from the node_modules/bootstrap directory? import "bootstrap/dist/css/bootstrap.rtl.min.css"; function MyApp({ Component, pageProps }) { return ( <> <Script src="node_m ...

Divide a JSON dataset into two distinct JSON files and incorporate them within the code

My JSON file structure is as follows: { "rID": "1", "rFI": "01", "rTN": "0011", "rAN": "11", "sID&quo ...

Looking to screen usernames that allow for the use of the DOT (.), underscore (_), and Dash (-)?

I am using jQuery to filter usernames that are exactly 3 characters long. The validation criteria includes only allowing the following special characters: ., _, and - var filter = /^[a-zA-Z0-9]+$/; Therefore: if (filter.test(currentval)) { //valid ...

Leveraging body parameters within an ExpressJS post Route

Currently, I am utilizing ExpressJS and MongoDB with a NodeJS Runtime backend, but I am facing a challenge in sending previously filled out form data to the next page for further steps in the form. The form consists of multiple pages. For instance, when cr ...

Struggling with flipping div functionality in IE9 on flipping content demo

I recently developed a flipping div using transitions and 3D transforms based on the Flipping Content Demo 1 from . While this works smoothly on most browsers, unfortunately IE 9 does not support transitions and 3D transforms. In order to address this iss ...

Having difficulty accessing data in a JavaScript file within Odoo 9 platform

I have attempted to extract HTML content from JavaScript declared in my module. However, when I try to retrieve content by class name, all I can access is the header contents and not the kanban view. openerp.my_module = function(instance) { var heade ...

Creating an AngularJs directive to alter input formatting

I am looking to achieve the following: Within my controller model, I have a date object that I want users to be able to modify. I need to provide them with two input fields - one for modifying the date and the other for modifying the time. Both input fiel ...

Encountering a CORS issue when attempting to establish a connection with S3 from a Rails application utilizing TextureLoader

I've been struggling with this issue for a while now. Despite searching on various search engines, I haven't made much progress. I have a script that loads a texture onto a sphere. var loader = new THREE.TextureLoader(); //allow cross origin ...

PHP and MySQL combination for efficient removal of multiple dropdown choices

I'm struggling to figure this out. Essentially, it's a form with multiple dropdown menus where users can select one or more items to filter MySQL data results. I want to dynamically update the other dropdown menus as the user makes selections. Fo ...

There was an error that occurred when attempting to read the property 'hasOne' of an undefined value: TypeError

Error Alert: Property 'hasOne' is undefined and cannot be read in Function.ADataMembers.associate. Check out ADataMember.js below: 'use strict' module.exports = (sequelize, DataTypes) => { var ADataMembers = sequelize.define(' ...

Display the hover effect for the current card when the mouse is over it

When the mouse hovers over a card, I want only that specific card to show a hover effect. Currently, when I hover over any card, all of them show the hover effect. Is there a way to achieve this in Vue Nuxtjs? Here's what I am trying to accomplish: ...