aws - 如何使用用户的名字或姓氏查询用户的字符?

时间:2018-01-19 04:38:35

标签: mysql amazon-web-services aws-lambda amazon-dynamodb

所以我有这个应用程序,我正在尝试实现一个搜索栏,如果他们输入“Jo”,我会调用一个lambda函数,它会向我询问用户:来自我用户表的“John Smith”。我创建了一个findRivalTable,在那里,对于每个创建的玩家,我创建了2个对象。每个对象都有一个键:firstTwoLetters,它包含第一个OR名字的firstTwo字母。第二个键是具有用户ID的用户的第一个OR姓氏。

我附上了下表的图片,以便更清楚。我不知道如何在dynamoDB查询中实现“startsWith”。我已经在javascript中编写了查询代码的开头但需要帮助才能完成它。它将用户输入的字母作为INPUT,然后查询应该返回表中的两个对象,然后我可以从用户的表中查询用户。

var dynamodb = new AWS.DynamoDB.DocumentClient(); 
exports.handler = function(event, context, callback) 
{
    completeCount = 0;
    completeCeiling = 2;

    function findPlayer() 
    {
        var params = {
            TableName:"Z_DEV_findPlayerTable",
            Key:{
                "firstLetters": event.inputCharacters
            },
        };

        dynamodb.update(params, function(err, data) 
        { if (err) {
            console.log("break 1A")
                console.log(err, "findPLayer error");
                callback(err);
            } else {
                console.log("break 1B");
                completeCount++;
                if (completeCount === completeCeiling) {
                    //cool we have the player! Now lets do a second 
                      query from the user's table from the ID we just got
                }

            }
        });
    }   


};

enter image description here

1 个答案:

答案 0 :(得分:0)

想出来!所以这是以下语法:

  1. 用户类型" Jo"对于John Smith,当您的数据库中有多个John Smiths时。您需要传入两个变量。
  2. 一个。他们输入的前两个字母。

    湾他们输入的所有内容。

    exports.handler = function(event, context, callback) 
    {
        completeCount = 0;
        completeCeiling = 1;
    
        var returnArray = {
        }
    
        findPlayer();
    
        function findPlayer() 
        {
            var params = 
            {
                TableName : "Z_DEV_findPlayerTable",
                KeyConditions: 
                {
                    "firstLetters": {
                        ComparisonOperator: 'EQ',
                        AttributeValueList: [
                            event.first2Letters ]
                    },
                    "firstOrLastName": 
                    {
                        ComparisonOperator: 'BEGINS_WITH',
                        AttributeValueList: [
                            event.currentTypedLetters ]
                    }
                },
            };
    
            dynamodb.query(params, function(err, data) 
            { if (err) {
                console.log("break 1A")
                    console.log(err, "findPLayer error");
                    callback(err);
                } else {
                    console.log("break 1B");
                    console.log("my data is", data.Items);
                    var folderKeys = Object.keys(data.Items);
                    completeCeiling = folderKeys.length;
                    for(i = 0; i < folderKeys.length; i++)
                    {
                        var filteredPlayer = JSON.stringify(data.Items[folderKeys[i]].firstOrLastName);
                        var splitString = filteredPlayer.split('+');
                        var filteredID = splitString[1];
                        filteredID = filteredID.replace(/['"]+/g, '')
    
                        console.log("my filteredPlayer is ", filteredPlayer);
                        console.log("my filteredID2 is ", filteredID);
                        getPlayer(filteredID);
                    }
    
                }
            });
        }
    
         function getPlayer(playerID) {
    
            var params = {
                TableName : "Z_DEV_playerTable",
                Key: {
                    "playerID": playerID
                },
            };
            dynamodb.get(params, function(err, data)
                { if (err) {
                    console.log(err);
                    console.log("break 2A");
                } else 
                {
                    console.log("break 2B");
                    //console.log("my data.Item is ",data.Item)
                    if(data.Item != undefined)
                    {
                        returnArray[playerID] = data.Item
                        completeCount++;
                        console.log("completeCount is " + completeCount);
                        if (completeCount === completeCeiling) 
                        {
                            callback(null, JSON.stringify(returnArray)); 
                        }
                    }
                    else
                    {
                        console.log("player doesn't exist");
                    }
                }
            });   
        }
    };