Creating a TypeScript schema with nested maps and arrays using Dynamoose

I'm currently in the process of developing a schema for a specific example:

{
    "foods": [
        {
            "fruits": [{
                "apple": {
                    "color": "red",
                    "shape": "round"
                }
            }]
        }
    ]
}

Initially, I attempted the following structure, which theoretically should work but encounters a bug as identified in https://github.com/dynamoose/dynamoose/issues/909.

export const FoodsSchema = new dynamoose.Schema({
  foods: {
    type: Array,
    schema: [{
      type: Object,
      schema: {
        fruits: {
          type: Array,
          schema: [{
            apple: {
              type: Object,
              schema: {
                color: String,
                shape: String,
              },
            },
          ],
        },
      },
    }],
  },
});

However, it appears to have some issues. I encountered a

TypeError: Cannot read property 'toLowerCase' of undefined
. This error is likely due to the presence of nested arrays or objects.

In addition, I came across

TypeMismatch: Expected foods.0.fruits to be of type object, instead found type object.
. When attempting to modify the schema in an effort to address this issue, it was not the solution.

  • This was observed in version 2.7.0.

Answer №1

Resolution: Separate the nested schema into a distinct schema and reference it within the initial one.

const bananaSchema = new dynamoose.Schema({
  banana: {
    type: Object,
    schema: {
      size: String,
      type: String,
    },
  },
});

Subsequently, in the original schema:

        fruits: {
          type: Array,
          schema: [bananaSchema],
        },

Answer №2

Consider an alternative approach by modifying your schema to the following structure:

export const FoodsSchema = new dynamoose.Schema({
  foods: {
    type: Array,
    schema: [{
      type: Object,
      schema: {
        fruits: {
          type: Array,
          schema: [{
            type: Object,
            schema: {
              apple: {
                type: Object,
                schema: {
                  color: String,
                  shape: String,
              },
            },
          ],
        },
      },
    }],
  },
});

(Hopefully that is accurate).

In the previous fruits schema part, there was an array followed by apple:

schema: [
  apple: {

I may be mistaken, but that doesn't seem like valid JavaScript syntax.

The main idea is that you can achieve what you want within one schema. Alternatively, you could split it into smaller objects rather than schemas for simplicity.

As mentioned in another response, dividing it into schemas is also a viable option!

There are multiple approaches to solving this issue.

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

Unable to retrieve the updated value from the service variable

I'm attempting to implement a filter that allows me to search for items based on a service variable that is updated with user input. However, I am only able to retrieve the initial value from the service in my component. Service HTML (whatever is typ ...

Is it possible to utilize Webpack 5's ChunkGroup API with several entries?

I am encountering an error message when attempting to upgrade from Webpack 4 to Webpack 5. The error states: Module.entryModule: Multiple entry modules are not supported by the deprecated API (Use the new ChunkGroup API) I have searched for information o ...

The main module's postinstall process is initiated before the sub-module's postinstall process

Update: I am seeking guidance on how to install a module from GitHub instead of npm. That's the main query. In case you're wondering why: I'm currently working on some confidential projects and prefer not to publish the code. As a result, ...

Does having an excessive amount of variable declarations result in a noticeable decline in performance?

One thing I notice for the sake of readability is that I tend to create new variables for data that I already have on hand. I'm curious, does this impact performance significantly? Here's an example of what I mean: const isAdult = this.data.per ...

What's the best way to ensure you're linting the correct file type for importing in Web

Upon installation of WebStorm, I encountered an issue when opening an existing Vue/TypeScript project. The IDE was not correctly importing and linting some file imports that were functioning properly through ESLint/webpack. In my usage of Vue 2.x with com ...

"Put Jest to the test by running it with the Express

Currently, I am in the process of learning how to build an API with TypeScript and experimenting with testing it using the Jest framework. When utilizing a basic Express application like app = express() supertest(app) everything works smoothly. However, ...

Support for ViewEncapsulation.ShadowDom now available in Edge, Internet Explorer, and legacy browsers

I am working with Angular 7 and material design. Some of my components utilize ShadowDOM ViewEncapsulation, leading to errors in older versions of IE, Edge, Chrome, and Firefox. Below is the error message I am encountering: Object doesn't support pr ...

Looking for a button that can be toggled on and off depending on the input fields

Even after adding useEffect to my code, the button component remains disabled unless the input fields are filled. It never enables even after that. export default function Page() { const [newPassword, setNewPassword] = useState(''); const [conf ...

Transferring documents from an angular ionic application to a slim php framework on the localhost

Hey there! I've got a project on my localhost where I need to upload files to a local folder. I'm sharing the code here in hopes that someone can assist me. HTML: <ion-item ion-item *ngFor="let item of lista" menuClose> Floor: ...

How can I call a method from a class using Typescript when getting an error saying that the property does not exist on the

Below is a service definition: export class MyService { doSomething(callbacks: { onSuccess: (data: Object) => any, onError: (err: any) => any }) { // Function performs an action } } This service is utilized in a component as shown be ...

Switching over to Typescript: Sending mapped properties

I'm having trouble converting my React.JS script to TypeScript. I need assistance in creating a drop-down navigation bar on my website. Here's a snippet from my Header.tsx file: The error message Property 'onClick' does not exist on t ...

Having trouble with Angular 2's Output/emit() function not functioning properly

Struggling to understand why I am unable to send or receive some data. The toggleNavigation() function is triggering, but unsure if the .emit() method is actually functioning as intended. My end goal is to collapse and expand the navigation menu, but for ...

The HttpInterceptor is programmed to identify and capture 401 error responses

After successfully implementing a code that called a logout() method upon receiving a 401 response from the server, I encountered issues following an upgrade of Angular from 5.2 to 7.0.3. It seems like either the HttpInterceptor interface has been modified ...

Customize the MUISelect style within a universal theme

I need to override a specific style for numerous components, but it is currently only working for all components except the Select. Here is what I am attempting: MuiSelect: { styleOverrides: { select: { ...

The types for Cypress are not being detected by my Angular tsconfig file

I'm facing an issue with my Angular tsconfig not detecting the Cypress 12.3 types. I have tried numerous solutions to resolve this problem, but nothing seems to work, except for the extreme measure of starting the project over, which I think might sol ...

Tips for preventing the need to cast a DOM element to any in Typescript

In my Typescript code, I am retrieving the value of a DOM element like this: document.getElementById('MyElementId') as HTMLElement).value I feel unsure about casting it to HTMLElement. Is there a better way to specify the type and retrieve this ...

Angular 11 Working with template-driven model within a directive

My currency directive in Angular 8.2 formats currency fields for users by using the following code: <input [(ngModel)]="currentEmployment.monthlyIncome" currency> @Directive({ selector: '[ngModel][currency]', providers: [Curr ...

The problem with the onClick event not triggering in Angular buttons

My issue revolves around a button in my code that is supposed to trigger a function logging the user out. However, for some reason, the event associated with it doesn't seem to be functioning at all. Here's the relevant code snippet: TS File: imp ...

Conceal object from inventory upon clicking

As someone who is new to React and Typescript, I am facing challenges in understanding how to hide a ticket from the list when the hide button is clicked. displayTickets = (tickets: Ticket[]) => { const filteredTickets = tickets.filter(t => ...

I am interested in creating a class that will produce functions as its instances

Looking to create a TypeScript class with instances that act as functions? More specifically, each function in the class should return an HTMLelement. Here's an example of what I'm aiming for: function generateDiv() { const div = document.crea ...