0

I'm trying to create a schema for the following example:

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

I was initially trying to do the below structure, which is technically correct and should work but fails due to the bug found 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 does not seem to work. Was getting TypeError: Cannot read property 'toLowerCase' of undefined. The lowerCase error occurs because of the nested arrays or objects.

I also saw TypeMismatch: Expected foods.0.fruits to be of type object, instead found type object. The latter occurred when I tried to change the schema thinking that may be it, but it was not the problem.

  • This was using version 2.7.0.

2 Answers 2

5

Solution: Extract the nested schema into it's own schema and reference that in the original one.

const appleSchema = new dynamoose.Schema({
  apple: {
    type: Object,
    schema: {
      color: String,
      shape: String,
    },
  },
});

and then in the original one:

        fruits: {
          type: Array,
          schema: [appleSchema],
        },
Sign up to request clarification or add additional context in comments.

Comments

1

Another option would be to change your schema to be the following:

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 correct).

Originally inside of the fruits schema property you had an array then apple:

schema: [
  apple: {

I could be wrong, but I don't even think that is valid JavaScript.

Point being, you can for sure do what you are trying to within 1 schema. You can also break it into smaller objects instead of schemas to make it easier.

However, as mentioned in the other answer, breaking it into schemas works as well!

Lots of different ways to achieve this.

2 Comments

If the code I listed above still produces errors on the latest version, please file bugs against the Dynamoose repo so that I can work on getting those fixed at some point.
Yes, that is a syntax error on my part from removing work specific details. I have fixed that in the original question. Thank you for pointing it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.