进行nodeJS查询调用时如何在MySQL存储过程中循环

时间:2019-05-08 08:44:50

标签: javascript mysql node.js axios koa

我正在构建一个调度程序应用程序,允许我向日历中添加约会。但是,我遇到了一个问题,即我的存储过程永远不会以我希望的方式循环到我的React项目中。发生的是,下面的代码仅插入一个元素,并且如果我将重复值设置为7(一周一次),则不会循环。我有一个日期范围选择器来选择从2019-05-05到2019-05-30的范围,重复的数字将是x天数(在此示例中为7)并每7天插入一个约会。 / p>

我正在使用axios从我的JS文件中调用路由

      axios.post(`/AddTime/${idValue}/${dayValue}/${startValue}/${endValue}/${timeValue}/${startDate}/${endDate}/${zero}/${zero}/${zero}/${repeat}`)

然后转到我的路由器(nodeJS和Koa):

   AdvisingRouter.post('/AddTime/:id/:day/:starttime/:endtime/:timeblock/:startdate/:enddate/:islocked/:takenPlace/:oldAppointment/:repeating', AdvisingController.addTimes, (err) => console.log("routers.js: AdvisingRouter error:", err));

然后调用实际查询:

async addTimes(ctx) {
    console.log("THIS IS IN ADVISING CONTROLLER");
    return new Promise((resolve, reject) => {
    let query = "call repeating (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    console.log('About to run this query.', query);
        dbConnection.query(
            {
                sql: query,
                values: [ctx.params.id, ctx.params.day, ctx.params.starttime, ctx.params.endtime, ctx.params.timeblock, ctx.params.startdate, ctx.params.enddate, ctx.params.islocked, ctx.params.takenPlace, ctx.params.oldAppointment, ctx.params.repeating]
            }, (error) => {
                if (error) {
                    return reject(error);
                }
            }

        )
    }).catch(err => {
        ctx.body = {
            status: "Failed",
            error: err,
            user: null
        };
    });
}

查询肯定在MySql中有效:

       DELIMITER //
       CREATE PROCEDURE repeating(
       _id int, _Day varchar(45), _StartTime varchar(45), _EndTime 
       varchar(45), _TimeBlock varchar(45), 
       _startDate date, _endDate date, _islocked tinyint, _takenPlace 
       tinyint, _oldAppointment tinyint, _repeatedNumber int)
       begin
        -- Add into AdvisingTimes and possibly repeat evey so often
        Declare __for int;
        Declare __INCDATE date;
        DECLARE __id int;
        DECLARE __Day varchar(45);
        DECLARE __StartTime varchar(45);
        DECLARE __EndTime varchar(45);
        DECLARE __TimeBlock varchar(45);
        DECLARE __startDate date;
        DECLARE __endDate date;
        DECLARE __islocked tinyint;
        DECLARE __takenPlace tinyint;
        DECLARE __oldAppointment tinyint;
        DECLARE __repeatedNumber int;

        select _id into __id;
        select _Day into __Day;
        select _StartTime into __StartTime;
        select _EndTime into __EndTime;
        select _TimeBlock into __TimeBlock;
        select _startDate into __INCDATE;
        select _endDate into __endDate;
        select _islocked into __islocked;
        select _takenPlace into __takenPlace;
        select _oldAppointment into __oldAppointment;
        select _repeatedNumber into __repeatedNumber;



        IF __repeatedNumber = 0 THEN
            Insert into AT (id, Day, StartTime, EndTime, TimeBlock, startDate, endDate, islocked, takenPlace, oldAppointment)
            values (__id, __Day, __StartTime, __EndTime, __TimeBlock, __startDate, __startDate, __islocked, __takenPlace, 2);
        ELSE
            WHILE __INCDATE <= _endDate DO
                Insert into AT (id, Day, StartTime, EndTime, TimeBlock, startDate, endDate, islocked, takenPlace, oldAppointment)
                values (__id, __Day, __StartTime, __EndTime, __TimeBlock, __INCDATE, __endDate, __islocked, __takenPlace, 4);
                SET __INCDATE = (select DATE_SUB(__INCDATE, INTERVAL -__repeatedNumber DAY));
            END WHILE;
        END IF;

结束// 分隔符;

我尝试在内部保存变量,但这也不起作用。它将获取一次值并将其发送,但是当其重复时,它将重置所有值。因此,仅插入一条记录,而不是循环并插入应该每7天设置在日历上的重复约会(基本上重复一次)。我不知道这一切是否有意义,但是当我在MySQL WorkBench中使用上面的查询时,它可以工作,但是当我从react应用程序中调用它时,它就无法工作。

0 个答案:

没有答案