根据更新的字段调用函数

时间:2017-09-09 14:21:23

标签: node.js rest express ecmascript-6

我想根据更新的模型字段调用不同的函数。 我的代码如下:

update(req, res){
    return LED
        .findById(req.params.LEDId)
        .then(LED => {
            if (!LED) {
                return res.status(400).send({
                    message: 'LED Not Found',
                });
            }
            return LED
            .update(req.body, {fields: Object.keys(req.body)})
            .then(() => res.status(200).send(LED))
            .catch((error) => res.status(400).send(error));
        })

        .catch((error) => res.status(400).send(error));
},

所以我的计划是整合一些if子句来获取更改的值,并根据更改调用一些函数。

如果子句:

if(req.body.status || LED.status){
    BLE.changeStatus(req.body.device_ID,req.body.status);
}else if(req.body.prog || LED.prog){
    BLE.changeProg(req.body.device_ID,req.body.prog);
}else if(req.body.white || LED.white){
    BLE.changeWhite(req.body.device_ID,req.body.white);
}else if(req.body.color || LED.color){
    BLE.changeColor(req.body.device_ID,req.body.color);
}else if(req.body.brightness || LED.brightness){
    BLE.changeBrightness(req.body.device_ID,req.body.brightness);
}

我需要在哪里集成这些可以调用函数的if子句? 我在将字段更新发送到DB之前尝试将其集成到.then()中,但在尝试启动服务器时出现以下错误:

SyntaxError: Unexpected token if
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/pi/projekt/server/controllers/index.js:1:75)
[nodemon] app crashed - waiting for file changes before starting...

修改

我现在更进一步.. 我已经写了更新函数,如:

update(req, res){
        return LED
            .findById(req.params.LEDId)
            .then(LED => {
                if (!LED) {
                    return res.status(404).send({
                        message: 'LED Not Found',
                    });
                }

                if(req.body.status){
                    changeStatus(req.params.LEDId,req.body.status);
                    console.log('STATUS CHANGED');
                } if(req.body.prog){
                    changeProg(req.params.LEDId,req.body.prog);
                    console.log('PROG CHANGED');
                } if(req.body.white){
                    changeWhite(req.params.LEDId,req.body.white);
                    console.log('WHITE CHANGED');
                } if(req.body.color){
                    changeColor(req.params.LEDId,req.body.color);
                    console.log('COLOR CHANGED');
                } if(req.body.brightness){
                    console.log('BEFORE BRIGHNTESS CHANGED')
                    changeBrightness(req.params.LEDId,req.body.brightness)
                    console.log('BRIGHNTESS CHANGED')
                }

                return LED
                .update(req.body, {fields: Object.keys(req.body)})
                .then(() => res.status(200).send(LED))
                .catch((error) => res.status(400).send(error));
            })
        .catch((error) => res.status(400).send(error));
    },

我已经测试了它并且它跳转到changeBrightness函数。在那里,我想与贵族合作。代码如下所示:

changeBrightness(LEDId,updateBrightness){
console.log('BEGINN CHANGEBRIGHTNESS FUNCTION')
        var uuid = "4711";
        var brightness = updateBrightness;
console.log('BRIGHTNESS', brightness)
console.log('UUID', uuid)
console.log('AFTER CHANGEBRIGHTNESS VAR')
        // Connect to client, find Service, find Characteristic and write status
        noble.connect(uuid, function(error){
            noble.discoverServices([lightningServiceUuid], function(error, service){
                var tempLightningService = service[0];
                writeFile("SUCCESS -- Discovered Service on UUID");

                tempLightningService.discoverCharacteristics([brightnessCharacteristic], function(error, characteristics){
                    var tempBrightnessCharacteristic = characteristics[0];
                    writeFile("SUCCESS -- Discovered Characterisitc on UUID");
console.log('IN THE MIDDLE OF CHANGEBRIGHTNESS FUNCTION')
                    tempBrightnessCharacteristic.write(new Buffer(brightness), true, function(error){
                        writeFile("SUCCESS -- Wrote brightness characteristic");
                    });
                });
            });
        });
        //Disconnect from client
        noble.disconnect(function(error){
            writeFile("SUCCESS -- Disconnected from Client");
        });
console.log('END CHANGEBRIGHTNESS FUNCTION')
    }

我目前正在使用假uuid进行测试..所以当我启动服务器并执行更新请求时,我的输出看起来像这样:

[nodemon] restarting due to changes...
[nodemon] starting `node ./bin/www`
Executing (default): SELECT "id", "device_id", "name", "group", "status", "device_type", "prog", "white", "color", "brightness", "createdAt", "updatedAt" FROM "LEDs" AS "LED" WHERE "LED"."id" = '1';
BEFORE BRIGHNTESS CHANGED
BEGINN CHANGEBRIGHTNESS FUNCTION
BRIGHTNESS 5
UUID 4711
AFTER CHANGEBRIGHTNESS VAR
PUT /api/led/1 400 357.728 ms - 2

为什么它在崇高的功能之前就停止了?我需要改变什么?

0 个答案:

没有答案