[英]Why is the view counter showing undefined?
我完全迷路了,我已经尝试了将近 2 天了。 这是我的代码,如果您需要更多信息,请告诉我。 我需要休息一下,所以我不是最好的。 我为此做了一个帐户,所以我没有足够的状态来发布嵌入的图片。
JS 代码作为文本
const countUrl = 'https://igf5ctozsa.execute-api.us-east-1.amazonaws.com/default/updateViews'
const countElement = document.getElementById('count');
updateViewCount();
function updateViewCount() {
fetch(countUrl)
.then(res => res.json())
.then(res => {
countElement.innerText = res.val;
});
}
Python API 作为文本
import json, boto3
client = boto3.client('dynamodb')
TableName = 'cloud-resume-stats'
def lambda_handler(event, context):
response = client.update_item(
TableName='cloud-resume-stats',
Key = {
'stats': {'S': 'viewcount'}
},
UpdateExpression = 'ADD Quantity :inc',
ExpressionAttributeValues = {":inc" : {"N": "1"}},
ReturnValues = 'UPDATED_NEW'
)
val = int(response['Attributes']['Quantity']['N'])
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*'
},
'body': val
}
API 响应为文本
Test Event Name
TestEvent
Response
{
"statusCode": 200,
"headers": {
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*"
},
"body": 147
}
Function Logs
START RequestId: 25413898-aca7-45e7-869e-41dd94879af4 Version: $LATEST
END RequestId: 25413898-aca7-45e7-869e-41dd94879af4
REPORT RequestId: 25413898-aca7-45e7-869e-41dd94879af4 Duration: 75.93 ms Billed Duration: 76 ms Memory Size: 512 MB Max Memory Used: 63 MB Init Duration: 339.61 ms
Request ID
25413898-aca7-45e7-869e-41dd94879af4
根据 Python API Response 的 body 部分的值,我认为res
只是147
,而不是res.val
。
fetch(countUrl)
.then(res => res.json())
.then(res => {
//countElement.innerText = res.val; // res is 147
countElement.innerText = res;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.