#Lambda function increments by 2 instead of 1

19 messages · Page 1 of 1 (latest)

lapis yoke
#

hello everyone. my name is jay. i've gotten to the lambda section of the challenge and encountered a minor but annoying bug:

when i load lambda URL, instead of increasing the view by 1, it's increasing it by 2. i've tried leaving it as just views = views but that just keeps the current number. the views are being counted in the dynamodb table as well. what am i missing? i've rewritten the code line by line and i still can't wrap my head around it. here's my code. i'm sorry if i spoil it for anyone:

import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('cloudresume')
def lambda_handler(event, context):
response = table.get_item(Key={
'id':'0'
})
views = response['Item']['views']
views = views +1
print(views)
response = table.put_item(Item={
'id' : '0',
'views' : views
})

return views
rugged crag
#

Is it possible you are calling the function twice?

#

Throw a print views in there before you increment as well.

#

And look at options other than put_item. I think there might be an update.

grizzled vapor
#

well in your code your only doing it twice

#

i would agree with @rugged crag maybe inside your Javascript code is being called twice somehow

leaden summit
#

Is it happening when you call this from frontend or does it happen even if you test the lambda directly? If you are using React sometimes due to component reloads an api gets called twice..

lapis yoke
#

@rugged crag i placed the print(views) before the increment and still had the same result of incrementing by 2.
@grizzled vapor what do you mean i'm only doing it twice? also, i have not started the javascript portion. ironically, i wanted to avoid starting the javascript portion before the lambda portion was complete to avoid something like this happening lol
@leaden summit i am testing the lambda directly. i figured it should work on it's own flawlessly before i try tying it to javascript

rugged crag
#

Are you looking in your lambda logs? You should have a value for each print in your logs

lapis yoke
rugged crag
#

Times are uct. I think EDT is a -5hour offset. So that seems right

#

also when you print, you can make them a little more clear for yourself temporarily by doing something like print(f"before - {views}") views += 1 print(f"after - {views}")

lapis yoke
leaden summit
#

If u copied that code..add the end brackets in each print line

rugged crag
#

Lol. Whoops. On mobile. Edited

lapis yoke
leaden summit
#

What does the before and after print statements print

rugged crag
#

It's just string formatting

#

The after one is two more?