StackExchange.Redis Lua script error in for loop

2019-05-29 07:39发布

问题:

I am facing a problem with the Lua script (please find it below) in the StackExchange.Redis. I have dissected the code and got all of it working except for the last loop. I get the following as error -- ERR Protocol error: expected '$', got ' ' The error message is quite generic in nature, as it gives me the same error message for different types of errors. Can someone help me to figure out the problem?

var doesExist = (bool)db.ScriptEvaluate(@" 
                                        local function isDateInRange(DateStart, DateEnd, GivenDate)
                                            if (GivenDate > DateStart and GivenDate < DateEnd)
                                            then
                                                return(true)
                                            else
                                            return(false)
                                        end
                                    end

                                    --Fetch User's Category's StartDate and EndDate
                                    local function IsCategoryDateValid()
                                    local givenDate, DateStart, DateEnd

                                    GivenDate = '2017-01-09'
                                    StartDate = '2015-01-01'
                                    EndDate = '2018-01-01'

                                    local isDateValid
                                    isDateValid = isDateInRange(StartDate, EndDate, GivenDate)
                                    return(isDateValid)
                                    end

                                    -- Pass User, UserCategory and UserCategoryItem as parameters
                                    local userNameKey = 'UserDetails:Invincible'
                                    local userCategoryKey = 'UserCategories:Book'
                                    local userCategoryItemKey = 'UserCategoryItems:Harry-Potter'

                                    local userNameKeySplit = {}
                                    local a = 1
                                    for i in string.gmatch(userNameKey, '([^'..':'..']+)') do
                                      userNameKeySplit[a] = i
                                      a = a + 1
                                    end
                                    local userName = userNameKeySplit[2]


                                    local userCategoryKeySplit = {}
                                    local b = 1
                                    for j in string.gmatch(userCategoryKey, '([^'..':'..']+)') do
                                      userCategoryKeySplit[b] = j
                                      b = b + 1
                                    end
                                    local userCategory = userCategoryKeySplit[2]



                                    local userCategoryItemKeySplit = {}
                                    local c = 1
                                    for k in string.gmatch(userCategoryItemKey, '([^'..':'..']+)') do
                                      userCategoryItemKeySplit[c] = k
                                      c = c + 1
                                    end
                                    local userCategoryItem = userCategoryItemKeySplit[2]


                                    -- Fetch Users, UserCategories and UserCategoryItems from the Redis DB
                                    local userData = {'Invincible', 'User1', 'User2', 'User3'}
                                    local userCategoryData = {'Book', 'Movie', 'Journals'}
                                    local userCategoryItemData = {'Hardy-Boys', 'Harry-Potter', 'Shawshank-Redemption', 'The-Terminal'}

                                    local msg


                                    for i=1,#userData,1
                                            do
                                                if(userData[i] == userName)
                                                then
                                                    for j=1,#userCategoryData,1
                                                    do
                                                        if(userCategoryData[j] == userCategory)
                                                        then
                                                            msg = true
                                                        break
                                                        else
                                                            msg = false
                                                        end
                                                    end             
                                                break
                                                else
                                                    msg = false
                                                    break
                                                end
                                            end


                                    return msg





                                    ",

            new RedisKey[] { "UserDetails:" + "Invincible", "UserCategories:" + "Book", "UserCategoryItems:" + "Harry-Potter" });