简体   繁体   English

我需要向我的 Javascript 添加什么才能显示来自我的 DynamoDB 表和 API 网关的命中计数器?

[英]What do I need to add to my Javascript to be able to show the hit counter from my DynamoDB table and API Gateway?

I want to be able to retrieve the hit counter from my DynamoDB table.我希望能够从我的 DynamoDB 表中检索命中计数器。 I have an API Gateway, Lambda, and DynamoDB table all working with each other correctly.我有一个 API 网关、Lambda 和 DynamoDB 表都可以正常工作。 So far the code I have hits the API, triggering a count in the dynamo table, however the count on my site stays at 0. I'm very new to Javascript so I was hoping I could get some guidance for this.到目前为止,我已经点击 API 的代码,触发了发电机表中的计数,但是我网站上的计数保持在 0。我对 Javascript 很陌生,所以我希望我能得到一些指导。 Here's the code I have so far:这是我到目前为止的代码:

const countEl = document.getElementById('count');

updateVisitCount();

function updateVisitCount() {
    fetch('https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/prod/')
    .then(res => res.json())
    .then(res => {
        countEl.innerHTML = res.value;
    })
}

My Lambda Function:我的 Lambda Function:

import json
import os

import boto3

ddb = boto3.resource('dynamodb')
table = ddb.Table(os.environ['HITS_TABLE_NAME'])
_lambda = boto3.client('lambda')


def handler(event, context):
    print('request: {}'.format(json.dumps(event)))
    table.update_item(
        Key={'path': event['path']},
        UpdateExpression='ADD hits :incr',
        ExpressionAttributeValues={':incr': 1}
    )

    resp = _lambda.invoke(
        FunctionName=os.environ['DOWNSTREAM_FUNCTION_NAME'],
        Payload=json.dumps(event),
    )

    body = resp['Payload'].read()

    print('downstream response: {}'.format(body))
    return json.loads(body)

Thanks for your help!谢谢你的帮助!

how often is the data on dynamodb updated? dynamodb 上的数据多久更新一次?

the easy workaround is calling updateVisitCount() every N seconds.简单的解决方法是每 N 秒调用updateVisitCount()

You can try with this code.您可以尝试使用此代码。 It will update value every 5 seconds它将每 5 秒更新一次值

const countEl = document.getElementById('count');

function updateVisitCount() {
    fetch('https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/prod/')
    .then(res => res.json())
    .then(res => {
        countEl.innerHTML = res.value;
    })
}

setInterval(updateVisitCount, 5000)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的 JavaScript 计数器显示? - How do I get my JavaScript counter to show? 如何在我的 javascript 游戏中添加计数器? - How do I add a counter in my javascript game? 每次点击按钮添加答案时,我需要在脚本中更改什么以将 A 列添加到 B 列返回 B 列中的答案? - What Do I Need to Change in My Script to Add Column A to Column B Returning the Answer in Column B Each Time I Hit My Button to Add Them? 我需要一个javascript圆形计数器和我的游戏得分计数器 - I need a javascript round counter and score counter for my game 我需要向我的 javascript 添加什么才能使用本地存储,以便在刷新页面时,用户选择的字体大小仍然相同? - What do I need to add to my javascript to be able use local storage so that when the page is refreshed, the user-selected font-size is still the same? 我需要添加什么,所以我的JavaScript会像我想要的那样工作? - What do i need to add, so my JavaScript would work as i want it to? 我无法为我的计数器项目添加颜色 - i am not able to add color to my counter project 为什么我需要在我的javascript方法中添加单词this - Why Do I need to add the word this to my javascript method 我无法在mysql中将信息添加到我的表中。 我使用javascript显示信息消息,然后它什么也不做 - i can not add informations to my table at mysql. i use javascript to show an information message and it doesnot do anything then 使用API​​网关将数据从DynamoDb表转换为HTML表 - Getting data from a DynamoDb table into an HTML table using API Gateway
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM