简体   繁体   English

尽管能够使用不同的数组写入数据库以 PUT 到数据库,但 DynamoDB 抛出未定义值错误

[英]DynamoDB throwing undefined values error despite being able to write to DB using different array to PUT to DB

I have a method that's making a call to a table with the following schema: userId: string |我有一个方法调用具有以下架构的表:userId: string | sessionId: string | sessionId: 字符串 | orders: list Here's the following code: orders: list 下面是代码:

const marshallOptions = {
    convertEmptyValues: false,
    removeUndefinedValues: false,
    convertClassInstanceToMap: false,
  };
  
const unmarshallOptions = {
    wrapNumbers: false,
  };
  
const translateConfig = { marshallOptions, unmarshallOptions };

const client: DynamoDBClient = new DynamoDBClient({});
const dynamo: DynamoDBDocument = DynamoDBDocument.from(client, translateConfig);

async function handleOrder(event: APIGatewayProxyEventV2): Promise<string> {
    let req: any = JSON.parse(String(event?.body));
    // Get student's orders
    let studentOrderSubmissions: any = await dynamo.send(new GetCommand({
        TableName: orderBook,
        Key: {
            userId: req.userId,
            sessionId: sessionIdPlaceholder
        }
    }))
    let studentOrders: any = studentOrderSubmissions.Item?.orders;
    const orderInfo: OrderInfo = {
        orderNumber: req.orderNumber,
        userId: req.userId,
        price: req.price,
        position: req.position,
        isLong: req.isLong,
        isBuy: req.isBuy,
        quantity: req.quantity,
        closedOut: req.closedOut
    } 
    studentOrders.push(orderInfo);
    const params = {
        TableName: orderBook,
        Item: {
            userId: req.userId,
            sessionId: req.sessionId,
            orders: studentOrders
        }
    };
    await dynamo.put(params, function(err: any, data: any) {
        if (err) {
            console.log("Error", err);
        } 
        else {
            console.log("Success", data);
        }
    });

When modifying the studentOrders array pushing a new student order into it, after trying to PUT the changes into DynamoDb into the OrderBook table, I always get the following error: Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.在修改 studentOrders 数组以将新的学生订单推入其中时,在尝试将更改放入 DynamoDb 到 OrderBook 表中后,我总是收到以下错误: Pass options.removeUndefinedValues=true to remove undefined values from map/array/set. EVEN after setting marshall options.removeUndefinedValues to false (despite it by default being false).即使在将 marshall options.removeUndefinedValues 设置为 false 之后(尽管它默认为 false)。

When I create a new array with the following code:当我使用以下代码创建一个新数组时:

const testArray: any = [];
testArray.push(orderInfo);

and replace studentOrders with testArray, DynamoDB saves the data and doesn't throw an error.并将 studentOrders 替换为 testArray,DynamoDB 会保存数据并且不会抛出错误。 Is this because DynamoDB doesn't allow you to pass in an array from itself?这是因为 DynamoDB 不允许您从自身传入数组吗? How would one update a list attribute in a table then?那么如何更新表中的列表属性呢? If I can't use put or PutCommand that leaves me with the query options and writing a new query but I figured I'd ask more experienced people first if they've ever run into this problem.如果我不能使用putPutCommand这会给我留下查询选项并编写一个新查询,但我想我会先询问更有经验的人是否遇到过这个问题。

Found out what the issue was.找出问题所在。 My object wasn't undefined.我的 object 不是未定义的。 What was undefined was my sort key.未定义的是我的排序键。 It was throwing an error letting me know one of my values being passed into the parameters was undefined but didn't tell me which one specifically: With that being said the error was here:它抛出了一个错误,让我知道我传递给参数的值之一是未定义的,但没有告诉我具体是哪一个:话虽如此,错误在这里:

Item: {
   userId: req.userId,
   sessionId: req.sessionId,
   orders: studentOrders
}

I never defined req.sessionId, SO THAT was what was causing the freak out.我从未定义过 req.sessionId,所以这就是导致异常的原因。 It really should've been cannot pass undefined KEY rather than just say that you can't pass an undefined value.它真的应该是 cannot pass undefined KEY 而不是仅仅说你不能传递一个未定义的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js 用于管理的控制台应用程序代码 Azure Cosmos db 教程代码抛出错误为未定义 - node.js console app code to manage Azure Cosmos db tutorial code throwing error as undefined AWS IoT 核心数据到 dynamodb 并非所有数据都在数据库中接收 - AWS IoT core data to dynamodb not all data is being received in the db Firebase 实时数据库:如果要写入的值尚未存储在数据库中,如何设置规则以仅写入数据库 - Firebase Realtime DB: How to setup rule to only write to DB if value being passed to be written is not already stored in DB 使用 SQL 的 Cosmos DB 查询数组值 - Cosmos DB Query Array value using SQL DynamoDB DocumentClient.put 意外返回 DynamoDB.putItem 错误 - DynamoDB DocumentClient.put returns DynamoDB.putItem error unexpectedly DynamoDB 事务:写入具有动态名称的不同表 - DynamoDB Transactions : Write to different tables with dynamic names 使用 IN 子句查询 dynamodb 数据库列表项 - Query dynamodb db list items with IN clause 如何使用 python 将项目放入 dynamodb 表中? - How to put an item into a dynamodb table using python? 如何在 cosmos db 中编写类似于 Mongo DB 聚合的聚合 - How to write aggregations in cosmos db similar to Mongo DB aggregations Firebase 在数据库中发布之前检查是否存在值数组 - Firebase check if an array of values exist before posting in DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM