gpt4 book ai didi

node.js - 使用 NodeJS Promise 在 DynamoDB 中创建记录

转载 作者:行者123 更新时间:2023-12-02 15:46:25 25 4
gpt4 key购买 nike

我想使用 NodeJS 函数在 dynamoDB 中创建一条新记录,但在我需要使用 stripe 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

最佳答案

问题是 recordSub 没有返回 promise 。您需要添加 .promise() 以使 DocumentClient 做出 promise :

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

另外,你确定你的表名和id正确吗? SUBRISCPTIONS 不是 SUBSCRIPTIONS 的正确拼写。

关于node.js - 使用 NodeJS Promise 在 DynamoDB 中创建记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61253990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com