在for循环表中查询dynamodb会得到一个空对象

时间:2019-09-01 17:38:42

标签: node.js aws-lambda amazon-dynamodb alexa-skill dynamodb-queries

我已经设置了一个动态表,如下所示,它显示一个房间是否空闲。

Time(Primary key)         g02 110

Monday 7                 English
Monday 7.5               English
Monday 8                 Free
Monday 8.5               Free

我正在查询此表以使用for循环内的主键单独获取数据(以查看7月7日星期一是否空闲)。

node.js处理程序的特定功能

const GetRoomAvailableIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'getRoomAvailable';
    },


    handle(handlerInput) {

        const { responseBuilder } = handlerInput;
        const slots = handlerInput.requestEnvelope.request.intent.slots;
        const room = slots.building.value + " " + slots.room.value;
        var day = slots.day.value;
        var StartTime = slots.startTime.value;
        var EndTime = slots.endTime.value;
        var x = new Array()
        console.log("1st:", room, day, StartTime, EndTime)


        //Spliting Hrs and Mins from slot value

        var StartTime1 = parseFloat(StartTime.slice(1, 2), 10)
        var StartTime2 = parseFloat(StartTime.slice(3, 5), 10)
        var EndTime1 = parseFloat(EndTime.slice(1, 2), 10)
        var EndTime2 = parseFloat(EndTime.slice(3, 5), 10)

        //Rounding Mins

        if (StartTime2 < 30) Startime2 = 0.0;
        else Startime2 = 0.5
        if (EndTime2 < 30) EndTime2 = 0.0;
        else EndTime2 = 0.5


        StartTimeNum = StartTime1 + Startime2
        EndTimeNum = EndTime1 + EndTime2

        console.log("2nd:", StartTime1, StartTime2, EndTime1, EndTime2)
        var checkAvailability = true
        if (StartTimeNum < EndTimeNum) {
            for (var i = StartTimeNum; i < EndTimeNum; i = i + 0.5) {
                var Time = day + " " + i.toString();
                console.log("3rd :", Time)
                var y = dbHelper.getRoomDetails2(Time, room)
                console.log("4th  :", y)
                if (y != "Free") {
                    checkAvailability = false; break;
                }

                /*dbHelper.getRoomDetails(Time)
                .then((data) => {
                console.log(data.map(e => e[room]))
                    if(data.map(e => e[room]) != "Free"){
                    checkAvailability = false

                    }

                })*/
            };

            if (checkAvailability) {
                var speechText = room + " is free on " + day + " from " + StartTime + " to " + EndTime + ". Would you like to book the room?"
                const attributes = handlerInput.attributesManager.getSessionAttributes();
                attributes.Room = room;
                attributes.Time = Time;
                attributes.handlerFunction = "room"
            }
            else {
                var speechText = "Unfortunately! " + room + " is not free on " + day + " from " + StartTime + " to " + EndTime + "."
            }
            console.log("Final: ", speechText)

            return responseBuilder
                .speak(speechText)
                .reprompt(GENERAL_REPROMPT)
                .getResponse();

        }
        else
            return responseBuilder
                .speak("End time is greater than start time. Please try again with a valid time")
                .reprompt(GENERAL_REPROMPT)
                .getResponse();
    }
}

这是数据库在另一个js文件dbHelper中提供帮助的代码

dbHelper.prototype.getRoomDetails2 = (time, room) => {
    console.log("DBHelper :", room, time)

    var params = {
        TableName: tableName_room,
        KeyConditions: {
            "Time": {
                ComparisonOperator: 'EQ',
                AttributeValueList: [time,]
            },
        },
    };

    docClient.query(params, function (err, data) {
        if (err) {
            console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
        }
        else {
            console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
            return (data[room])
        }
    })
}

当我使用

的广告位值运行此代码时
building - g02 ,
room - 110 , 
startTime - 08:00 , 
endTime - 09:00 , 
day - Monday 

返回

    START RequestId: 71a22ba7-22ca-477a-9c31-6048d737be41 Version: $LATEST
2019-09-01T17:08:58.755Z 71a22ba7-22ca-477a-9c31-6048d737be41 1st: g02 110 Monday 08:00 09:00
2019-09-01T17:08:58.755Z 71a22ba7-22ca-477a-9c31-6048d737be41 2nd: 8 0 9 0
2019-09-01T17:08:58.755Z 71a22ba7-22ca-477a-9c31-6048d737be41 3rd : Monday 8
2019-09-01T17:08:58.756Z 71a22ba7-22ca-477a-9c31-6048d737be41 DBHelper : g02 110 Monday 8
2019-09-01T17:08:58.875Z 71a22ba7-22ca-477a-9c31-6048d737be41 4th : undefined
2019-09-01T17:08:58.875Z 71a22ba7-22ca-477a-9c31-6048d737be41 Final: Unfortunately! g02 110 is not free on Monday from 08:00 to 09:00.
2019-09-01T17:08:59.379Z 71a22ba7-22ca-477a-9c31-6048d737be41 GetItem succeeded:
{
    "Items": [],
    "Count": 0,
    "ScannedCount": 0
}

0 个答案:

没有答案