Discovering and including them one by one in an array through recursion

Within my database, I have a table that includes the following data structure:

https://i.sstatic.net/ckJR9.png

The table contains a column named parentIdeaId, which is used to reference the ID in the ideas table itself.

Below is the unstructured data currently stored in the database, which needs to be queried under specific conditions:

[{
    "id": "1",
    "title": "Title 1",
    "parentIdeaId": null
},{
    "id": "2",
    "title": "Title 2",
    "parentIdeaId": 1
},{
    "id": "3",
    "title": "Title 3",
    "parentIdeaId": 2
},{
    "id": "4",
    "title": "Title 4",
    "parentIdeaId": null
}]

To retrieve and filter this data as needed, I have created two functions. The first function retrieves data based on the provided ID:

function find({ id }) {
  return prisma.idea.findUnique({
    where: {
      id
    }
  });
}

The second function finds children of a parent idea based on the parentIdeaId:

function responseIdeas({ parentIdeaId }) {
  return prisma.idea.findMany({
    where: {
      parentIdeaId
    }
  });
}

The desired outcome is for only linked data (based on their parentIdeaId) to be retrieved. For example, querying the ID "1" should yield the following result:

[{
    "id": "1",
    "title": "Title 1",
    "parentIdeaId": null
},{
    "id": "2",
    "title": "Title 2",
    "parentIdeaId": 1
}{
    "id": "3",
    "title": "Title 3",
    "parentIdeaId": 2
}]

However, when running the code to achieve this, the output differs from expectations. It is structured in a way that was not intended. Here's the current implementation:

// JavaScript code snippet goes here

In the resulting JSON data, the structure is not as expected. This discrepancy leaves me wondering where exactly I went wrong in my implementation. Any insights would be greatly appreciated. Thank you.

Answer №1

discoverOffspring has the potential to yield an array of arrays because it utilizes Promise.all() for its return -> creating an array composed of promises, some of which may contain arrays.

To consolidate the results into a single array, you can utilize the concat method as shown in this example https://codesandbox.io/s/quizzical-sun-c4khx?file=/src/index.js

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

What methods can be used to identify the css properties that are overriding ReactJS component styles?

:) I've been working on creating a website for some time now, and I decided to incorporate a react-calendar component from this link. Initially, when the page was simpler, it worked perfectly as intended and displayed like this based on the example p ...

How can I remove a specific item from obj?

Is there a way to create a function called removeStar that removes the star key from an object and outputs it in the format shown by the expectedOutput variable? let input = { "p": { "pk1": "pv1", "pk2": "pv2", "c": { "*": { ...

Structured information in JSON format

I am working with a JSON file in Node.js to store data. The format I want the data to be saved in is like this: { "name": "No One", "age": 26 } However, currently the data is being saved like this: {"name": "No One", "age": 26} Every new piece ...

Incorrect credentials trigger an error in Nodemailer

Currently, I am utilizing nodemailer to handle email submissions from a registration form. Below is the code for my registration form: <form action="/registration" method="post"> <h3 class="text-center" style="font-family: 'champagne-l ...

Issue with jQuery functionality occurring only upon clicking and affecting just a single element

I'm having some trouble making this code work properly. The issue seems to be that it only works for one link, loading index.php?act=old... $(document).ready(function(){ $("#example").dialog({modal: true}); $("#example").dialog({widt ...

Enhance the database with partial updates using the patch method in Django Rest Framework

I have a model called CustomUser that extends the AbstractUser class. class CustomUser(AbstractUser): detail = models.JSONField(default=dict) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=Tr ...

Having issues with $_POST not retrieving values from .post

Below is the javascript snippet that I have written: function submitForm() { var name = document.getElementsByName('name').value ,email = document.getElementsByName('email').value ,subject = document.getElementsBy ...

Tips for maintaining the same identifier, content, or text despite changes in the DOM

I am curious about a situation where the ID of a div is changed from 1 to 34, but when clicked on, the attribute still returns 1 instead of 34. I apologize for any language errors, thank you. $(document).on('click','.edit_btn:eq(0)& ...

When navigating to a new route using history.push in React, it's important to ensure that the correct state is

My goal is to implement a smooth exiting animation with framer motion based on the user's current route and next destination. I specifically want the background to slide away only when transitioning from route A to route D. To achieve this, I decided ...

Tips for converting JSON object values to a string using JavaScript

Consider the following JSON object: { "id": 1, "name": "Name", "surname": "Surname", "number": "111111111" } Is there a way to transform this JSON object into a string that formats it as follows using JavaScript? Name Surname 111111111 ...

The focal length in THREE.js PerspectiveCamera seems to be miscalculated, which is not aligning with the field of view

When working with THREE.js, one of the fundamental aspects is constructing a camera using a specific function: const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000); An interesting aspect to consider regarding cameras is the relations ...

What are some creative ways to conceal text using CSS or JavaScript?

Looking for a solution: <div id="body" role="main"> <h1>Title</h1> <p>My site is mysite.com</p></div> I always have <div id="body" role="main">, <h1>, and <p> tags on each page. Is there a way to ...

Utilizing multiple local images effectively within a Next.js 13 component: Best practices for implementation

Currently working on a Next.js project and utilizing the Image component to showcase images. Familiar with importing a single local image like so: import Image from 'next/image'; import profilePic from '../public/me.png'; export defaul ...

What is the process for creating an array of allocatable scalars in Fortran?

Fortran 90 and newer versions allow for the use of allocatable arrays. INTEGER, ALLOCATABLE, DIMENSION(:) :: test_int_array Allocatable scalars, such as characters, are supported in Fortran 2003. CHARACTER(LEN=:), ALLOCATABLE :: test_str I'm curio ...

JavaScript code altered link to redirect to previous link

I added a date field to my HTML form. <input type="date" name="match_date" id="matchDate" onchange="filterMatchByDate(event)" min="2021-01-01" max="2021-12-31"> There is also an anchor tag ...

Partial view encountering Dropzone JS function error

I am currently using jquery-modal by Kyle Fox and I've run into a problem. Whenever I try to open the modal window, my partial view gets loaded into it. However, I keep seeing this error in the console: Dropzone.options.dropzone is not recognized a ...

Issues with Datatable Methods arise following the addition of a Row using append

I am facing a challenge that needs to be resolved. I have been using .append() to add new rows with input fields. However, the issue arises when these newly appended rows do not respond to any methods such as search or responsive buttons. Despite numerous ...

Converting Arrays in PHP

Can you transform the following array: Array ( [0] => Array ( [Contact] => Array ( [number] => 0425 234 634 ) ) [1] => Array ( [Conta ...

ElegantFrameload - Displaying an HTML webpage

I want to implement a monetary conversion tool on my webpage using fancy box. However, I am facing issues with getting it to work. Here is the link to the page that is not functioning properly: The link at the bottom of the page should trigger the fancy b ...

Use angular js to dynamically load a template via an ajax request

I am trying to send an AJAX request in order to retrieve a JSP/HTML page for the templateUrl of a modal. Here is the code I have written: var modalInstance = $uibModal.open({ animation: $scope.animationsEnabled, te ...