node.js - 从 DynamoDB 检索值

标签 node.js amazon-web-services amazon-dynamodb

我想从 DynamoDB 检索项目名称,这是我的表属性之一。

这是我当前的代码:

export async function main(event, context) {
  const data = JSON.parse(event.body);

  const params = {
    TableName: "dev-table",

    KeyConditionExpression: "userId = :userId" ,
    ConditionExpression: "projectId = :projectId",
    ExpressionAttributeValues: {
      ":userId": event.requestContext.identity.cognitoIdentityId,
      ":projectId": data.projectId
    }
  };

  try{
    const result = await dynamoDbLib.call("query", params);
    return success(result.Items);
  }catch(e){
    return failure(e);
  }
}

但是我收到一个错误:

Value provided in ExpressionAttributeValues unused in expressions: keys: {:projectId}

这个错误从何而来?
如何从表中检索单个属性值?

最佳答案

ConditionExpression 是 deleteItem、putItem、transactWriteItems 和 updateItem 方法的有效参数。对于查询方法,请对非关键属性使用 FilterExpression 参数:

export async function main(event, context) {
const data = JSON.parse(event.body);
const params = {
        TableName: "dev-table",
        KeyConditionExpression: "userId = :userId",
        FilterExpression: "projectId = :projectId",
        ExpressionAttributeValues: {
            ":userId": event.requestContext.identity.cognitoIdentityId,
            ":projectId": data.projectId
        }
    };

    try {
        const result = await dynamoDbLib.call("query", params);
        return success(result.Items);
    } catch(e) {
        return failure(e);
    }
}

关于node.js - 从 DynamoDB 检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58511366/

相关文章:

javascript - GraphQL - 将非特定对象的对象作为参数传递

node.js - 使用 watson-developer-cloud nodejs sdk 删除多个意图

amazon-web-services - DynamoDB 一对多和多对多结构

ios - 如何在 swift 4 中获取 ISO8601 格式?

java - 如何使用 Java 设置 DynamoDB 返回的匹配项的限制?

node.js - 使用 Nodejs 进行 DynamoDB 条件更新

Node.JS + Mongoose 回调 hell

mysql - AWS RDS + INTO OUTFILE

amazon-web-services - 使用 moto + serverless 模拟 DynamoDB

javascript - smooch-bot-example 不切换到另一个 block