You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Amplify GraphQL提交表单报错:输入含未定义字段'kidney infection'

问题:GraphQL提交表单数据到DynamoDB时出现字段未定义错误

错误提示

The variables input contains a field name 'kidney infection' that is not defined for input object type 'CreateFormsInput'

我的GraphQL Schema定义

type Forms @model @auth(rules: [{ allow: public }]) {
  ...
  substances: [String]
  medications: [AWSJSON]
  condition_history: [AWSJSON]
  allergies: [AWSJSON]
  ...
}

提交数据的代码

const formResponse = await API.graphql(graphqlOperation(createForms, { input: tempForm }));

表单数据处理逻辑

  • 针对condition_history字段,我会把其中的对象转为JSON字符串(符合AWS要求),比如将{problem: "kidney infection"}转换为"{\"problem\":\"kidney infection\"}",该字段是数组类型,可包含多个这类字符串。
  • medications和allergies字段采用完全相同的处理方式,没有出现报错;且当condition_history为空数组时,提交也正常。

补充信息

自动生成的CreateForms mutation(精简版)

export const createForms = /* GraphQL */ `
  mutation CreateForms(
    $input: CreateFormsInput!
    $condition: ModelFormsConditionInput
  ) {
    createForms(input: $input, condition: $condition) {
      id
      facility_id
      ...
      medications
      condition_history
      ...
      createdAt
      updatedAt
    }
  }
`;

最终提交的tempForm对象(精简版)

{​
allergies: [ "{\"allergy\":\"\",\"details\":\"\"}" ],
...
condition_history: [ "{\"problem\":\"kidney infection\"}" ],
date_of_birth: "1966-01-04",
medications: [ "{\"medication\":\"\",\"dose\":\"\",\"frequency\":\"\"}" ]​,
smoking_history: "{\"has_smoked\":\"false\",\"currently_smokes\":\"\",\"cigarettes_per_day\":\"\",\"year_quit_smoking\":\"\"}",
}

未做JSON字符串化处理的原始tempForm对象

{
    ...
    "smoking_history": {
        "has_smoked": "false",
        "currently_smokes": "",
        "cigarettes_per_day": "",
        "year_quit_smoking": ""
    },
    "medications": [
        {
            "medication": "",
            "dose": "",
            "frequency": ""
        }
    ],
    "allergies": [
        {
            "allergy": "blah",
            "details": "sdsds"
        }
    ],
    "condition_history": [
        {
            "problem": "kidney infection"
        }
    ],
    "kidney infection": true #this might be the problem...
}

我怀疑未字符串化的原始对象里的"kidney infection": true字段是问题所在,想请教下我哪里处理错了?


内容的提问来源于stack exchange,提问作者Fresh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 08:10:28