我想使用NodeJS函数在dynamoDB中创建新记录,但是在我需要使用Stripe API创建事务并将信息存储在DynamoDB中之后。
我编写了这些代码行,但不会在dynamo中插入任何内容。有什么想法吗?
import * as Stripe from "stripe";
const randomBytes = require('crypto').randomBytes;
const AWS = require('aws-sdk');
import { SECRET_API_KEY } from "../config";
/** Config */
const stripe = new Stripe(SECRET_API_KEY);
const ddb = new AWS.DynamoDB.DocumentClient();
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()
}
});
}
function toUrlString(buffer) {
return buffer.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
问题是recordSub
没有返回承诺。您需要添加.promise()
以使DocumentClient做出承诺:
return ddb.put({
TableName: 'OG_SUBRISCPTIONS',
Item: {
OG_SUBRISCPTIONS_ID: subrisctionId,
ProductPlan: productPlan,
RequestTime: new Date().toISOString()
}
}).promise();