使用 NodeJS promise 在 DynamoDB 中创建记录
Using NodeJS promise to create records in DynamoDB
我想使用 NodeJS 函数在 dynamoDB 中创建一条新记录,但是在我需要使用条带创建事务之前 API 以及在我将信息存储到 DynamoDB 之后。
我写了这几行代码,但是什么都不会插入到dynamo中。
有什么想法吗?
import * as Stripe from "stripe";
const randomBytes = require('crypto').randomBytes;
const AWS = require('aws-sdk');
AWS.config.update({ region: 'eu-central-1' });
import { SECRET_API_KEY } from "../config";
/** Config */
const stripe = new Stripe(SECRET_API_KEY);
const ddb = new AWS.DynamoDB.DocumentClient();
AWS.config.update({ region: 'eu-central-1' });
export async function createCustomerAndSubscribeToPlan(
stripeToken: string,
email: string,
productPlan: string
): Promise<any> {
// create a customer
const customer = await stripe.customers.create({
email: email,
source: stripeToken
});
// retrieve created customer id to add customer to subscription plan
const customerId = customer.id;
// create a subscription for the newly created customer
const subscription = await stripe.subscriptions.create({
customer: customerId,
items: [{ plan: productPlan }]
});
/** Insert inside S3 Storage the user and validity */
const record = await recordSub(stripeToken, email, productPlan);
return subscription;
}
function recordSub(stripeToken, email, productPlan) {
const subrisctionId = toUrlString(randomBytes(16));
console.log('Putting inside db ' + subrisctionId);
return ddb.put({
TableName: 'OG_SUBRISCPTIONS',
Item: {
OG_SUBRISCPTIONS_ID: subrisctionId,
ProductPlan: productPlan,
RequestTime: new Date().toISOString()
}
}).promise();
}
function toUrlString(buffer) {
return buffer.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
感谢@Wyetro,我还修改了我以前的代码,添加了 AWS 配置,并将我的 lambda 函数移动到 DynamoDB 的同一 AWS 区域。
我还使用 AWS CloudWatch 丰富了我的 LambdaRole,现在我收到以下错误消息:
2020-04-23T06:49:40.673Z 79140900-41ff-41aa-bab6-7d1ca6aabec2 INFO Putting inside db NRc_xX_dhep6XSbts51Sxw
2020-04-23T06:49:40.691Z 79140900-41ff-41aa-bab6-7d1ca6aabec2 ERROR (node:7) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
END RequestId: 79140900-41ff-41aa-bab6-7d1ca6aabec2
REPORT RequestId: 79140900-41ff-41aa-bab6-7d1ca6aabec2 Duration: 1851.16 ms Billed Duration: 1900 ms Memory Size: 1024 MB Max Memory Used: 126 MB Init Duration: 593.69 ms
在AWS论坛上查了下,发现aws-sdk模块版本有问题,升级后问题依旧。
CloudWatch 中没有错误,但 table.
中有任何错误
START RequestId: 61b612b4-b9a1-40ce-b6e8-5d75493ba8ea Version: $LATEST
2020-04-23T07:52:08.224Z 61b612b4-b9a1-40ce-b6e8-5d75493ba8ea INFO Putting inside db EG8UiaSElpfis7MyWanBTg
END RequestId: 61b612b4-b9a1-40ce-b6e8-5d75493ba8ea
REPORT RequestId: 61b612b4-b9a1-40ce-b6e8-5d75493ba8ea Duration: 2039.72 ms Billed Duration: 2100 ms Memory Size: 1024 MB Max Memory Used: 129 MB Init Duration: 631.94 ms
问题是 recordSub
没有返回承诺。您需要添加 .promise()
以使 DocumentClient 做出承诺:
return ddb.put({
TableName: 'OG_SUBRISCPTIONS',
Item: {
OG_SUBRISCPTIONS_ID: subrisctionId,
ProductPlan: productPlan,
RequestTime: new Date().toISOString()
}
}).promise();
您似乎也没有设置 AWS 区域。
AWS.config.update({ region: 'us-east-1' }); // Set to whatever your region is
此外,您确定您的 table 姓名和 ID 正确吗? SUBRISCPTIONS
不是 SUBSCRIPTIONS
的正确拼写。
已解决。我在 recordSub 调用中错过了 "await"。
问题已编辑。
我想使用 NodeJS 函数在 dynamoDB 中创建一条新记录,但是在我需要使用条带创建事务之前 API 以及在我将信息存储到 DynamoDB 之后。
我写了这几行代码,但是什么都不会插入到dynamo中。 有什么想法吗?
import * as Stripe from "stripe";
const randomBytes = require('crypto').randomBytes;
const AWS = require('aws-sdk');
AWS.config.update({ region: 'eu-central-1' });
import { SECRET_API_KEY } from "../config";
/** Config */
const stripe = new Stripe(SECRET_API_KEY);
const ddb = new AWS.DynamoDB.DocumentClient();
AWS.config.update({ region: 'eu-central-1' });
export async function createCustomerAndSubscribeToPlan(
stripeToken: string,
email: string,
productPlan: string
): Promise<any> {
// create a customer
const customer = await stripe.customers.create({
email: email,
source: stripeToken
});
// retrieve created customer id to add customer to subscription plan
const customerId = customer.id;
// create a subscription for the newly created customer
const subscription = await stripe.subscriptions.create({
customer: customerId,
items: [{ plan: productPlan }]
});
/** Insert inside S3 Storage the user and validity */
const record = await recordSub(stripeToken, email, productPlan);
return subscription;
}
function recordSub(stripeToken, email, productPlan) {
const subrisctionId = toUrlString(randomBytes(16));
console.log('Putting inside db ' + subrisctionId);
return ddb.put({
TableName: 'OG_SUBRISCPTIONS',
Item: {
OG_SUBRISCPTIONS_ID: subrisctionId,
ProductPlan: productPlan,
RequestTime: new Date().toISOString()
}
}).promise();
}
function toUrlString(buffer) {
return buffer.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
感谢@Wyetro,我还修改了我以前的代码,添加了 AWS 配置,并将我的 lambda 函数移动到 DynamoDB 的同一 AWS 区域。 我还使用 AWS CloudWatch 丰富了我的 LambdaRole,现在我收到以下错误消息:
2020-04-23T06:49:40.673Z 79140900-41ff-41aa-bab6-7d1ca6aabec2 INFO Putting inside db NRc_xX_dhep6XSbts51Sxw
2020-04-23T06:49:40.691Z 79140900-41ff-41aa-bab6-7d1ca6aabec2 ERROR (node:7) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
END RequestId: 79140900-41ff-41aa-bab6-7d1ca6aabec2
REPORT RequestId: 79140900-41ff-41aa-bab6-7d1ca6aabec2 Duration: 1851.16 ms Billed Duration: 1900 ms Memory Size: 1024 MB Max Memory Used: 126 MB Init Duration: 593.69 ms
在AWS论坛上查了下,发现aws-sdk模块版本有问题,升级后问题依旧。 CloudWatch 中没有错误,但 table.
中有任何错误START RequestId: 61b612b4-b9a1-40ce-b6e8-5d75493ba8ea Version: $LATEST
2020-04-23T07:52:08.224Z 61b612b4-b9a1-40ce-b6e8-5d75493ba8ea INFO Putting inside db EG8UiaSElpfis7MyWanBTg
END RequestId: 61b612b4-b9a1-40ce-b6e8-5d75493ba8ea
REPORT RequestId: 61b612b4-b9a1-40ce-b6e8-5d75493ba8ea Duration: 2039.72 ms Billed Duration: 2100 ms Memory Size: 1024 MB Max Memory Used: 129 MB Init Duration: 631.94 ms
问题是 recordSub
没有返回承诺。您需要添加 .promise()
以使 DocumentClient 做出承诺:
return ddb.put({
TableName: 'OG_SUBRISCPTIONS',
Item: {
OG_SUBRISCPTIONS_ID: subrisctionId,
ProductPlan: productPlan,
RequestTime: new Date().toISOString()
}
}).promise();
您似乎也没有设置 AWS 区域。
AWS.config.update({ region: 'us-east-1' }); // Set to whatever your region is
此外,您确定您的 table 姓名和 ID 正确吗? SUBRISCPTIONS
不是 SUBSCRIPTIONS
的正确拼写。
已解决。我在 recordSub 调用中错过了 "await"。 问题已编辑。