我的桌子上有四种类型的实体:客户、卖家、产品和订单。除了订单,我对所有这些都有基本的 CRUD 操作。
我需要一个产品的价格属性来计算新订单的 finalPrice 来创建它;但是,我无法在我的 lambda 函数中检索此属性来使用它。
在这里,我展示了我的 lambda 函数以及我如何尝试从产品中检索信息以创建订单。
module.exports.createOrder = (event, context, callback) => {
const clientId = event.pathParameters.PK;
const sellerId = event.pathParameters.sellerPK;
const productId = event.pathParameters.productPK;
const reqBody = JSON.parse(event.body);
const orderId = uuidv4();
const prodParams = {
Key: {
PK: `SELLER#${sellerId}`,
SK: `PRODUCT#${productId}`
},
TableName:dataTable
};
const prodData = db.get(prodParams, function(err, data){
if(err) {
console.log(err)
}
else{
console.log(data)
}
})
const order = {
PK: `CLIENT#${clientId}`,
SK: `ORDER#${orderId}`,
type: "order",
sellerId: sellerId,
productId: productId,
clientId: clientId,
orderId: orderId,
quantity: reqBody.quantity,
finalPrice: reqBody.quantity * prodData.Item.price
};
return db
.put({
TableName: dataTable,
Item: order
})
.promise()
.then(() => {
callback(null, response(201, order));
})
.catch((err) => response(null, response(err.statusCode, err)));
};
我还展示了如何在 serverless.yml 上配置路径
createOrder:
handler: handler.createOrder
events:
- http:
path: /client/{PK}/seller/{sellerPK}/product/{productPK}/order
method: post
integration: LAMBDA