【问题标题】:Using NodeJS promise to create records in DynamoDB使用 NodeJS 承诺在 DynamoDB 中创建记录
【发布时间】: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


    【解决方案1】:

    问题是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
    

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

    【讨论】:

    • 不起作用。我试过了,但我在 DynamoDB 中没有记录。
    • @c3p0 编辑了我的答案。看起来您没有设置区域。表名可能拼写错误。我会尝试添加一些日志,看看 AWS sdk 抛出了什么错误。
    • 编辑了我的问题。我还修改了之前添加 AWS 配置的代码,并将我的 lambda 函数移动到 DynamoDB 的同一 AWS 区域。我还使用 AWS CloudWatch 丰富了我的 LambdaRole,但还没有。
    【解决方案2】:

    已解决。我在 recordSub 通话中错过了“等待”。 问题已编辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2018-10-06
      相关资源
      最近更新 更多