简体   繁体   中英

Why is the view counter showing undefined?

I'm completely lost, I've been trying to figure this out for almost 2 days now. Here is my code, if you need any more information please let me know. I need a break, so I'm not at my best. I made an account for this so I do not have enough status to post embedded pictures.

JS Code as Text

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 as Text

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 Response as Text

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

According to the body part's value of the Python API Response, I think res is just 147 , not the res.val .

fetch(countUrl)
    .then(res => res.json())
    .then(res => {
    //countElement.innerText = res.val; // res is 147
    countElement.innerText = res;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM