简体   繁体   English

GSI 上的 DynamoDB 查询返回 null 和 nodeJs

[英]DynamoDB query on GSI returns null with nodeJs

I am trying to query dynamo table using global index but it is returning null for some reason.我正在尝试使用全局索引查询发电机表,但由于某种原因它返回 null。 I do have a record that I can query on from console but not working with node js sdk.我确实有一条记录,我可以从控制台查询,但不能使用节点 js sdk。

heres my code:这是我的代码:

try {
    var queryParams = {
      IndexName: "gsi1-index",
      KeyConditionExpression: "gsi1 = :gsi1",
      ExpressionAttributeValues: {
        ":gsi1": userEmail,
      },
      TableName: usersTable,
    };
    await ddb.query(queryParams, (err, userData) => {
      // if (err || userData.Count === 0)
      //   return res.status(400).json({ data: "No information was found" });
      return res.status(200).json(userData);
    });
  } catch (err) {
    return res.status(400).json(err);
  }

I don't know what am i doing wrong exactly?我不知道我到底在做什么错? googled everywhere but my code is exactly the same as the others and all the examples到处搜索,但我的代码与其他代码和所有示例完全相同

You haven't posted a full code example, but I'll venture a guess.您还没有发布完整的代码示例,但我会冒险猜测。 I'm assuming you are running this code in an async lambda using the DDB DocumentClient API.我假设您使用 DDB DocumentClient API 在异步 lambda 中运行此代码。

The problem appears to be that you are using await and callbacks together.问题似乎是您正在同时使用await和回调。

   await ddb.query(queryParams, (err, userData) => {
      return res.status(200).json(userData);
    });

Try this instead试试这个

let data = await ddb.query(queryParams).promise();
// do something with data

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM