“将对象数组添加到突变react-graphql-apollo-mongoose时无法读取未定义的属性'get'”

问题描述 投票:0回答:2

仅当我将"Cannot read property 'get' of undefined"对象数组添加到突变中时,才出现product错误,如果我仅添加orderId Int,则没有错误。以下是更多信息:

模式

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
}

静音

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

解析器(带有猫鼬)

 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 }
    );
}

即使出错,数据库中的突变也成功,但是graphql返回了

{
  "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
  }
}
mongoose graphql react-apollo graphql-js
2个回答
1
投票

第一

静音

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

突变addOrder应该返回Order对象

[findOneAndUpdate方法插入新记录,但返回结果是可选的-可以使用returnNewDocument: true(默认值:false)-参见docs

第二

如果要插入嵌套/嵌入的实体-ProductInput的数组-您也需要插入/创建它们-请参见slightly related question-当然,这取决于猫鼬模式(未提供)。

[使用mqql解析器使mongose部件工作和“包装”比适应/更改猫鼬模式以反映grpahql类型/ resolver的更改要容易。


0
投票

感谢@xadm,我通过替换解析器解决了该错误:

 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
            };
          })
        };
      });
    }
© www.soinside.com 2019 - 2024. All rights reserved.