最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DynamoDB query with lambda (node.js) FilterExpression using contains - Stack Overflow

programmeradmin0浏览0评论

Here is the table structure. In posts column it is a list of strings
So I been reading and trying to use filterExpression with 'contains'. What documentation said in node.js just need to use a CONTAINS b where a can be a list and b for me is a string I am searching for.

var db = new doc.DynamoDB();

var params = {
    TableName: "WIT_Search",
    KeyConditionExpression: "#type = :tag and #key between :start AND :end",
    FilterExpression: "#tag contains :post",
    ExpressionAttributeNames: {
        "#type": "type",
        "#tag": "posts",
        "#key": "key",
    },
    ExpressionAttributeValues: {
        ":tag": "tag",
         ":start": tag+"_",
         ":end": tag+"_999",
         ":post": postID
    }
};

console.log(params);

db.query(params, function(err, data) {
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
        console.log("Query succeeded.");
        console.log('Count =',data.Items.length);

        if (data.Items.length > 0){
            console.log("post exists in tag");
        }else{
            console.log("post doesnt exist in tag");
        }
    }
});


Whenever I do testing it gives me this message:

  Unable to query. Error: {
      "message": "Invalid FilterExpression: Syntax error; token: \"contains\", near: \"#tag contains (\"",
      "code": "ValidationException",
    }

The weird thing on AWS Console it works without the problem, just using in lambda with node.js it gives this error.

Here is the table structure. In posts column it is a list of strings
So I been reading and trying to use filterExpression with 'contains'. What documentation said in node.js just need to use a CONTAINS b where a can be a list and b for me is a string I am searching for.

var db = new doc.DynamoDB();

var params = {
    TableName: "WIT_Search",
    KeyConditionExpression: "#type = :tag and #key between :start AND :end",
    FilterExpression: "#tag contains :post",
    ExpressionAttributeNames: {
        "#type": "type",
        "#tag": "posts",
        "#key": "key",
    },
    ExpressionAttributeValues: {
        ":tag": "tag",
         ":start": tag+"_",
         ":end": tag+"_999",
         ":post": postID
    }
};

console.log(params);

db.query(params, function(err, data) {
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
        console.log("Query succeeded.");
        console.log('Count =',data.Items.length);

        if (data.Items.length > 0){
            console.log("post exists in tag");
        }else{
            console.log("post doesnt exist in tag");
        }
    }
});


Whenever I do testing it gives me this message:

  Unable to query. Error: {
      "message": "Invalid FilterExpression: Syntax error; token: \"contains\", near: \"#tag contains (\"",
      "code": "ValidationException",
    }

The weird thing on AWS Console it works without the problem, just using in lambda with node.js it gives this error.

Share Improve this question asked Jul 19, 2017 at 14:21 Ivars AkmentinsIvars Akmentins 551 gold badge2 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Here is the correct syntax.

FilterExpression : 'contains (#tag, :post)'
发布评论

评论列表(0)

  1. 暂无评论