我陷入了通过 AppSync 上传时间戳的简单任务。
我制作了这个测试架构以便于演示:
type Mutation {
testCreateCar(input: TestCreateCarInput!): TestCar
@aws_cognito_user_pools
}
type TestCar {
regno: String!
country: String!
timestamp: Int
}
input TestCreateCarInput {
regno: String!
country: String!
timestamp: Int
}
这是我的解析器:
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"regno": $util.dynamodb.toDynamoDBJson($ctx.args.regno),
"country": $util.dynamodb.toDynamoDBJson($ctx.args.country),
},
"attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args)
}
这是我的 FE 中发出请求的函数:
export const createCar = async (carData: Car) => {
try {
await client.graphql({
query: mutations.testCreateCar,
variables: { input: {
regno: carData.regno,
country: carData.country,
timestamp: Date.now()
} }
})
} catch (err) {
throw err;
}
};
当我尝试上传汽车时,出现以下错误:
“变量‘时间戳’具有无效值。”
怎么会这样? Date.now() 返回一个数字,并且我已明确指定“时间戳”在我的架构中是数字类型。
非常感谢您的宝贵时间,谢谢!
在您的架构中,您声明时间戳是“Int”类型。目前的 unix 时间戳(即 Date.now() 返回的值)是 1708650175742,这超出了 Int 值可以处理的范围(INT_MAX 是 2147483647)。