Facing an issue with in the frontend, data retrieved from AWS DynamoDB using AWS Lambda though Function URL is not displayed

huangapple go评论94阅读模式
英文:

Facing an issue with in the frontend, data retrieved from AWS DynamoDB using AWS Lambda though Function URL is not displayed

问题

我正在尝试为我的作品集设置一个查看器计数器,使用AWS Dynamo DB和AWS Lambda,借助Function URL的帮助。AWS Lambda正常工作,但当使用JavaScript调用它时,它显示错误"Failed to Fetch",但Lambda函数正常工作,它获取并更新了DynamoDB。

一切都正常,我的DynamoDB表如下所示,忽略第一行图片描述在这里,我的Lambda代码如下:

import json
import boto3

dynamodb = boto3.resource('dynamodb')
ddbTableName = 'Cloud-resume'
table = dynamodb.Table(ddbTableName)

def lambda_handler(event, context):
    response = table.get_item(Key={'id': 'count'})
    count = response["Item"]["vistor_count"]
    new_count = str(int(count) + 1)
    response = table.update_item(
        Key={'id': 'count'},
        UpdateExpression='set vistor_count = :c',
        ExpressionAttributeValues={':c': new_count},
        ReturnValues='UPDATED_NEW'
    )
    return {'Count': new_count}

我添加了一个JavaScript代码来从DynamoDB中检索数据并使用Lambda,使用以下代码:

<script>
    fetch('xyz.lambda-url.us-east-1.on.aws/')
    .then(response => response.json())
    .then((data) => {
        document.getElementById('visitor_counter').innerText = data.Count;
    })
</script>

我创建了一个HTML标签来显示查看器计数:

<p id="visitor_counter">Viewer count</p>

我的Lambda正常工作,它在URL中显示输出,我只是看不到作品集中的查看器计数,我已经尝试调试了几个小时,请帮忙,谢谢!!!

英文:

I'm trying to setup a viewer count for my portfolio, using AWS dynamo DB and AWS lambda with the help of Function URL. the AWS lambda works fine but when it is called by using JavaScript it shows the error "Failed to Fetch" but the Lambda function works, it fetches and update the DynamoDB.

everything works fine my DynamoDB table looks like this ignore the first rowenter image description here my lambda code is this
I'm trying to setup a viewer count for my portfolio, using AWS dynamo DB and AWS lambda with the help of Function URL. the AWS lambda works fine but when it is called by using JavaScript it shows the error "Failed to Fetch" but the Lambda function works, it fetches and update the DynamoDB.import json

import boto3
dynamodb = boto3. resource(&#39;dynamodb&#39;)
ddbTableName = &#39;Cloud-resume&#39;
table = dynamodb.Table(ddbTableName)
def lambda_handler(event, context):   
    response = table.get_item(Key= {&#39;id&#39;: &#39;count&#39;}) 
    count = response[&quot;Item&quot;][&quot;vistor_count&quot;]
    new_count = str(int(count)+1) 
    response = table.update_item( 
        Key={&#39;id&#39;: &#39;count&#39;},
        UpdateExpression=&#39;set vistor_count = :c&#39;,
        ExpressionAttributeValues={&#39;:c&#39;:new_count},
        ReturnValues=&#39;UPDATED_NEW&#39;
    )
    return {&#39;Count&#39;:new_count}

I added a JavaScript code to retrieve the data from DynamoDB using lambda using this code

&lt;script&gt;
    		fetch(&#39;xyz.lambda-url.us-east-1.on.aws/&#39;) 
      		.then(response =&gt; response.json())
      		.then((data) =&gt; {
        	document.getElementById(&#39;visitor_counter&#39;).innerText = data.count
      		})
&lt;/script&gt;

I have created a HTML tag to show the viewer count
<p id="visitor_counter">Viewer count</p>

my lambda works fine, it shows the output in the URL, I just don't see the viewer count in the portfolio, i have been trying to debug this for hours please help , THANK YOU!!!!

答案1

得分: 1

Casing matters in objects, and your casing seems wrong. In your lambda, you return this response:

{'Count': new_count}

But in your JS code, you use count with a lower case c:

document.getElementById('visitor_counter').innerText = data.count

Change the casing of one or the other to match, for example your JS to this:

document.getElementById('visitor_counter').innerText = data.Count

Furthermore, you don't need to issue the GetItem request, just work with the UpdateItem:

response = table.update_item( 
        Key={'id': 'count'},
        UpdateExpression='set visitor_count = visitor_count + :c',
        ExpressionAttributeValues={':c': 1},
        ReturnValues='UPDATED_NEW'
    )

return {'Count': response['Attributes']['visitor_count']}

This will save you both on capacity costs but more importantly, latency.

英文:

Casing matters in objects, and your casing seems wrong. In your lambda, you return this response:

 {&#39;Count&#39;:new_count}

But in your JS code, you use count with a lower case c:

document.getElementById(&#39;visitor_counter&#39;).innerText = data.count

Change the casing of one or the other to match, for example your JS to this:

document.getElementById(&#39;visitor_counter&#39;).innerText = data.Count

Furthermore, you don't need to issue the GetItem request, just work with the UpdateItem:

response = table.update_item( 
        Key={&#39;id&#39;: &#39;count&#39;},
        UpdateExpression=&#39;set vistor_count = visitor_count + :c&#39;,
        ExpressionAttributeValues={&#39;:c&#39; : 1},
        ReturnValues=&#39;UPDATED_NEW&#39;
    )

return {&#39;Count&#39;:response[&#39;Attributes&#39;][&#39;visitor_count&#39;]}

This will save you both on capacity costs but more importantly, latency.

答案2

得分: 0

你有在浏览器开发工具中检查你的URL响应吗?它应该显示为一个网络请求,你可以看到从你的函数返回的实际数据。

话虽如此,看起来你正在返回:

{'Count': new_count}

大小写很重要,所以你的更新代码应该是:

document.getElementById('visitor_counter').innerText = data.Count

而不是

document.getElementById('visitor_counter').innerText = data.count
英文:

Have you checked to see in the browser dev tools what the response is from your URL? It should show up as a network request and you can see the actual data returned from your function.

That being said it looks like you're returning

{&#39;Count&#39;:new_count}

Casing matters here so your code to update should actually be

document.getElementById(&#39;visitor_counter&#39;).innerText = data.Count

not

document.getElementById(&#39;visitor_counter&#39;).innerText = data.count

huangapple
  • 本文由 发表于 2023年8月5日 00:39:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定