0

I got the "Cannot read property 'get' of undefined" error only when i add product Array of Objects to mutation, if i only add orderId Int there is no error. Below are more info:

Schema

type Order {
    orderId: Int
    product: [ProductInput]
}

input ProductInput {
    prodId: String
    value: [ValueInput]
}

type Product {
    prodId: String
    value: [Value]
}

type Value{
    name:String,
    props:String
}

input ValueInput{
    name:String,
    props:String
}

Mutation

 addOrder(orderId: Int, product: [ProductInput]):Order

Resolver (with mongoose)

 addOrder(parent, args, context, info) {
 let orderId;
 let product;

 if (args.orderId) {
      orderId = { orderId: args.orderId };
      }
 if (args.product) {
      product= { product: args.product};
      }

return context.Order.findOneAndUpdate(
      { orderId: args.orderId },
      {
        $setOnInsert: {
          ...orderId,
          ...product
        }
      },
      { new: true, upsert: true }
    );
}

Even I get error, the mutation is successfully in database,but graphql return

{
  "errors": [
    {
      "message": "Cannot read property 'get' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addOrder"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property 'get' of undefined",
            "    at EmbeddedDocument.<anonymous> (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\schema.js:1824:23)",
            "    at VirtualType.applyGetters (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\virtualtype.js:137:25)",
            "    at EmbeddedDocument.Document.get (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\document.js:1508:19)",
            "    at applyVirtuals (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\document.js:3246:20)",
            "    at EmbeddedDocument.Document.$toObject (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\document.js:2986:5)",
            "    at EmbeddedDocument.Document.toObject (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\document.js:3169:15)",
            "    at DocumentArrayPath.cast (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\schema\\documentarray.js:401:27)",
            "    at DocumentArrayPath.SchemaType.applySetters (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\schematype.js:1010:12)",
            "    at DocumentArrayPath.SchemaType._castForQuery (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\schematype.js:1424:15)",
            "    at DocumentArrayPath.SchemaType.castForQueryWrapper (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\schematype.js:1391:17)",
            "    at castUpdateVal (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\helpers\\query\\castUpdate.js:515:19)",
            "    at walkUpdatePath (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\helpers\\query\\castUpdate.js:342:22)",
            "    at castUpdate (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\helpers\\query\\castUpdate.js:99:18)",
            "    at model.Query._castUpdate (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\query.js:4481:10)",
            "    at castDoc (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\query.js:4509:18)",
            "    at model.Query.Query._findAndModify (C:\\Users\\mihai\\Desktop\\Projects\\ECOM\\BACKEND\\node_modules\\mongoose\\lib\\query.js:3472:22)"
          ]
        }
      }
    }
  ],
  "data": {
    "addOrder": null
  }
}

2 Answers 2

1

First

Mutation

 addOrder(orderId: Int, product: [ProductInput]):Order

Mutation addOrder should return Order object

findOneAndUpdate method inserts a new record but returning result is optional - it can be done using returnNewDocument: true (default: false) - see docs

Second

If you're inserting nested/embeded entities - array of ProductInput - you need to insert/create them, too - see slightly related question - of course it depends on mongoose schema (you didn't provided).

It's easier to have mongose parts working and 'wrap' with graphql resolvers than adapt/change mongoose schema to reflect grpahql types/resolver changes.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, {new:true} in mongoose i think is same as returnNewDocument in mongo. Anyway, i tried using returnNewDocument but still got error. Also i tried to create nested entity , also i got the same result (error, but succesfully mutation) : ``` if (args.product) { product = { product: args.product.map((product) => { return { prodId: product.prodId, variation: product.variation.map((variation) => { return { name: variation.name, value: variation.value }; }) }; }) }; }```
actually you are right: i adjusted schema like product.prodId, instead of {product:{prodId:x}} and i got no error now! Thanks! Also It's easier to have mongose parts working and 'wrap' with graphql resolvers than adapt/change mongoose schema to reflect grpahql types/resolver changes. Can you point me to something more detailed? I would love to learn a different and easier method
it shouldn't be required but probably helps [apollo] in recognizing types - defining __typename [for products and variants] property should do the trick ... show mongoose schema (how is related to graphql types)
0

Thanks to @xadm I solved the error via replacing in resolver:

 if (args.product) {
      product= { product: args.product};
      }

with

if (args.product) {
      product = args.product.map((product) => {
        return {
          'product.prodId': product.prodId,
          'product.variation': product.variation.map((variation) => {
            return {
              'product.variation.name': variation.name,
              'product.variation.value': variation.value
            };
          })
        };
      });
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.