DynamoDB.putItem function not saving item with sls command
up vote
0
down vote
favorite
I am having trouble with my Lambda function with Serverless framework, which should be putting data to DynamoDB. I'm unsure of the issue as I am getting no error message in the console when I run: sls invoke local -f scrape -d 'the-last-bookstore-los-angeles
.
saveRatingsToDB.js
const uuid = require('uuid');
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (data, businessName) => {
console.log('data saving...');
console.log(data);
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: uuid.v1(),
businessName: businessName,
reviewCount: data.reviewCount,
rating: data.rating,
createdAt: JSON.stringify(new Date())
}
};
// I am can log params okay, but nothing with dynamoDb.put is logged to the console
dynamoDb.put(params, error => {
console.log('putting data');
if (error) {
console.log(`Error saving data to DynamoDB: ${JSON.stringify(error)}`);
return Promise.reject(
`Error saving data to DynamoDB: ${JSON.stringify(error)}`
);
} else {
console.log('data saved');
return Promise.resolve(params.Item);
}
});
};
handler.js
'use strict';
const { getPage, parsePage, saveRatingsToDB } = require('./utils');
module.exports.scrape = async (event, context) => {
console.log('Function triggered', event);
const name = event;
const page = await getPage(name);
const data = await parsePage(page);
const db = await saveRatingsToDB(data, name);
return db;
};
serverless.yaml
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-2
environment:
DYNAMODB_TABLE: my-table-name
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn_reference_"
package:
include:
- utils/**
As mentioned, I get no error message in the console. I can the log and see the params
object from the saveRatingsToDB.js
, but nothing within the actual dynamoDb.put
function.
Any help would be appreciated.
node.js aws-lambda amazon-dynamodb serverless-framework
add a comment |
up vote
0
down vote
favorite
I am having trouble with my Lambda function with Serverless framework, which should be putting data to DynamoDB. I'm unsure of the issue as I am getting no error message in the console when I run: sls invoke local -f scrape -d 'the-last-bookstore-los-angeles
.
saveRatingsToDB.js
const uuid = require('uuid');
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (data, businessName) => {
console.log('data saving...');
console.log(data);
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: uuid.v1(),
businessName: businessName,
reviewCount: data.reviewCount,
rating: data.rating,
createdAt: JSON.stringify(new Date())
}
};
// I am can log params okay, but nothing with dynamoDb.put is logged to the console
dynamoDb.put(params, error => {
console.log('putting data');
if (error) {
console.log(`Error saving data to DynamoDB: ${JSON.stringify(error)}`);
return Promise.reject(
`Error saving data to DynamoDB: ${JSON.stringify(error)}`
);
} else {
console.log('data saved');
return Promise.resolve(params.Item);
}
});
};
handler.js
'use strict';
const { getPage, parsePage, saveRatingsToDB } = require('./utils');
module.exports.scrape = async (event, context) => {
console.log('Function triggered', event);
const name = event;
const page = await getPage(name);
const data = await parsePage(page);
const db = await saveRatingsToDB(data, name);
return db;
};
serverless.yaml
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-2
environment:
DYNAMODB_TABLE: my-table-name
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn_reference_"
package:
include:
- utils/**
As mentioned, I get no error message in the console. I can the log and see the params
object from the saveRatingsToDB.js
, but nothing within the actual dynamoDb.put
function.
Any help would be appreciated.
node.js aws-lambda amazon-dynamodb serverless-framework
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am having trouble with my Lambda function with Serverless framework, which should be putting data to DynamoDB. I'm unsure of the issue as I am getting no error message in the console when I run: sls invoke local -f scrape -d 'the-last-bookstore-los-angeles
.
saveRatingsToDB.js
const uuid = require('uuid');
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (data, businessName) => {
console.log('data saving...');
console.log(data);
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: uuid.v1(),
businessName: businessName,
reviewCount: data.reviewCount,
rating: data.rating,
createdAt: JSON.stringify(new Date())
}
};
// I am can log params okay, but nothing with dynamoDb.put is logged to the console
dynamoDb.put(params, error => {
console.log('putting data');
if (error) {
console.log(`Error saving data to DynamoDB: ${JSON.stringify(error)}`);
return Promise.reject(
`Error saving data to DynamoDB: ${JSON.stringify(error)}`
);
} else {
console.log('data saved');
return Promise.resolve(params.Item);
}
});
};
handler.js
'use strict';
const { getPage, parsePage, saveRatingsToDB } = require('./utils');
module.exports.scrape = async (event, context) => {
console.log('Function triggered', event);
const name = event;
const page = await getPage(name);
const data = await parsePage(page);
const db = await saveRatingsToDB(data, name);
return db;
};
serverless.yaml
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-2
environment:
DYNAMODB_TABLE: my-table-name
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn_reference_"
package:
include:
- utils/**
As mentioned, I get no error message in the console. I can the log and see the params
object from the saveRatingsToDB.js
, but nothing within the actual dynamoDb.put
function.
Any help would be appreciated.
node.js aws-lambda amazon-dynamodb serverless-framework
I am having trouble with my Lambda function with Serverless framework, which should be putting data to DynamoDB. I'm unsure of the issue as I am getting no error message in the console when I run: sls invoke local -f scrape -d 'the-last-bookstore-los-angeles
.
saveRatingsToDB.js
const uuid = require('uuid');
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (data, businessName) => {
console.log('data saving...');
console.log(data);
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: uuid.v1(),
businessName: businessName,
reviewCount: data.reviewCount,
rating: data.rating,
createdAt: JSON.stringify(new Date())
}
};
// I am can log params okay, but nothing with dynamoDb.put is logged to the console
dynamoDb.put(params, error => {
console.log('putting data');
if (error) {
console.log(`Error saving data to DynamoDB: ${JSON.stringify(error)}`);
return Promise.reject(
`Error saving data to DynamoDB: ${JSON.stringify(error)}`
);
} else {
console.log('data saved');
return Promise.resolve(params.Item);
}
});
};
handler.js
'use strict';
const { getPage, parsePage, saveRatingsToDB } = require('./utils');
module.exports.scrape = async (event, context) => {
console.log('Function triggered', event);
const name = event;
const page = await getPage(name);
const data = await parsePage(page);
const db = await saveRatingsToDB(data, name);
return db;
};
serverless.yaml
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-2
environment:
DYNAMODB_TABLE: my-table-name
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn_reference_"
package:
include:
- utils/**
As mentioned, I get no error message in the console. I can the log and see the params
object from the saveRatingsToDB.js
, but nothing within the actual dynamoDb.put
function.
Any help would be appreciated.
node.js aws-lambda amazon-dynamodb serverless-framework
node.js aws-lambda amazon-dynamodb serverless-framework
edited Nov 19 at 0:42
John Rotenstein
64.9k770113
64.9k770113
asked Nov 18 at 13:14
Tim Rooke
14010
14010
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You tried to run saveRatingsToDB
as an asynchronous function by doing:
const db = await saveRatingsToDB(data, name);
Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put
is because the saveRatingsToDB
function doesn't wait for the asynchronous callback to finish but return earlier.
To make saveRatingsToDB
properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:
module.exports = (data, businessName) => {
...
// use dynamoDb.put().promise() to return a promise from dynamoDb transaction
return dynamoDb.put(params, error => {
...
// Do whatever with the error
}).promise();
};
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You tried to run saveRatingsToDB
as an asynchronous function by doing:
const db = await saveRatingsToDB(data, name);
Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put
is because the saveRatingsToDB
function doesn't wait for the asynchronous callback to finish but return earlier.
To make saveRatingsToDB
properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:
module.exports = (data, businessName) => {
...
// use dynamoDb.put().promise() to return a promise from dynamoDb transaction
return dynamoDb.put(params, error => {
...
// Do whatever with the error
}).promise();
};
add a comment |
up vote
1
down vote
accepted
You tried to run saveRatingsToDB
as an asynchronous function by doing:
const db = await saveRatingsToDB(data, name);
Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put
is because the saveRatingsToDB
function doesn't wait for the asynchronous callback to finish but return earlier.
To make saveRatingsToDB
properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:
module.exports = (data, businessName) => {
...
// use dynamoDb.put().promise() to return a promise from dynamoDb transaction
return dynamoDb.put(params, error => {
...
// Do whatever with the error
}).promise();
};
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You tried to run saveRatingsToDB
as an asynchronous function by doing:
const db = await saveRatingsToDB(data, name);
Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put
is because the saveRatingsToDB
function doesn't wait for the asynchronous callback to finish but return earlier.
To make saveRatingsToDB
properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:
module.exports = (data, businessName) => {
...
// use dynamoDb.put().promise() to return a promise from dynamoDb transaction
return dynamoDb.put(params, error => {
...
// Do whatever with the error
}).promise();
};
You tried to run saveRatingsToDB
as an asynchronous function by doing:
const db = await saveRatingsToDB(data, name);
Unfortunately, with the way this function is written currently, it's not an asynchronous function. The reason while you don't see the log inside dynamoDb.put
is because the saveRatingsToDB
function doesn't wait for the asynchronous callback to finish but return earlier.
To make saveRatingsToDB
properly an asynchronous function and force it to wait for the callback, you can return a dynamoDb.put as a promise from that function:
module.exports = (data, businessName) => {
...
// use dynamoDb.put().promise() to return a promise from dynamoDb transaction
return dynamoDb.put(params, error => {
...
// Do whatever with the error
}).promise();
};
answered Nov 19 at 3:49
Thai Duong Tran
1,28858
1,28858
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361254%2fdynamodb-putitem-function-not-saving-item-with-sls-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown