[英]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.