GraphQL API提交订单报错:userID字段未在CreateOrderInput中定义
解决GraphQL提交订单时的字段不匹配错误
问题根源
错误提示"The variables input contains a field name 'userID' that is not defined for input object type 'CreateOrderInput'"的核心原因是字段大小写不匹配:
- 在
schema.graphql的Order类型中,关联字段定义为小写开头的驼峰格式:userId和carId - 前端提交的input对象里,使用了大写开头的
userID和carID,而GraphQL对字段名大小写严格区分,导致服务器无法识别这两个字段。
修复方案
修改前端提交代码中的字段名,与schema定义保持一致:
// 提交到服务器 try { const userInfo = await Auth.currentAuthenticatedUser(); const date = new Date(); const input = { createAt: date.toISOString(), type, originLatitude: originPlace.details.geometry.location.lat || 0, originLongitude: originPlace.details.geometry.location.lng || 0, destLatitude: destinationPlace.details.geometry.location.lat || 0, destLongitude: destinationPlace.details.geometry.location.lng || 0, userId: userInfo.attributes.sub, // 修正字段名为userId carId: "1", // 修正字段名为carId } const response = await API.graphql ( graphqlOperation ( createOrder, { input: input }, ) ) console.log(response); Alert.alert ( "Order has been submitted successful!!!", [{ text: "Go home", onPress: () => navigation.navigate('HomeScreen') }] ) } catch (e) { console.error(e); }
验证说明
修改后重新提交订单,服务器就能正确识别userId和carId字段,不会再抛出字段未定义的错误,订单可正常创建。
内容的提问来源于stack exchange,提问作者Andii Nguyen
相关产品推荐
相关产品推荐

