【发布时间】:2020-07-29 21:55:57
【问题描述】:
我想使用 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 中没有错误,但表中有任何错误。
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
【问题讨论】:
标签: node.js promise amazon-dynamodb put