1

edit: added a bit more code.

    const express = require('express');
    var bodyParser = require('body-parser');
    const app = express();

    var urlencodedParser = bodyParser.urlencoded({extended: false})

    const {google} = require('googleapis');
    const {PubSub} = require('@google-cloud/pubsub');
    const iot = require('@google-cloud/iot');
    const API_VERSION = 'v1';

    const DISCOVERY_API = 'https://cloudiot.googleapis.com/$discovery/rest';
    app.get('/', urlencodedParser, (req, res) => {

    const projectId = req.query.proyecto;
    const cloudRegion = req.query.region;
    const registryId = req.query.registro;
    const numSerie = req.query.numSerie;
    const command = req.query.command;

    const client = new iot.v1.DeviceManagerClient();
    if (client === undefined) {
        console.log('Did not instantiate client.');
    } else {
        console.log('Did instantiate client.');
        sendCom();
    }

    async function sendCom() {
        const formattedName = await client.devicePath(projectId, cloudRegion, registryId, numSerie)
        const binaryData = Buffer.from(command);
        const request = {
            name: formattedName,
            binaryData: binaryData,
        };
        return client.sendCommandToDevice(request).then(responses => res.status(200).send(JSON.stringify({
            data: OK
        }))).catch(err => res.status(404).send('Could not send command. Is the device connected?'));
    }
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
    console.log(`App listening on port ${PORT}`);
    console.log('Press Ctrl+C to quit.');
});

module.exports = app;

I have this function, that I call after the client initiate: sendCom();

     async function sendCom() {
        const formattedName = await client.devicePath(projectId, cloudRegion, registryId, deviceId)
        const binaryData = Buffer.from(command);            
        const request = { name: formattedName, binaryData: binaryData, };

        client.sendCommandToDevice(request)
        .then(responses => {
            res.status(200).send(JSON.stringify({ data: OK })).end();                
        })
        .catch(err => {
            res.status(404).send('Could not send command. Is the device connected?').end();             
        });
    }

My problem is that sendCommandToDevice gets executed perfectly, however I get the catch error. As I understand it, it's because in the .then ends the connection.

I've looked at this and thats's what I tried, however I'm not sure I understand what's going on.

1x2x3x4x
  • 592
  • 8
  • 26
  • 1
    you are using `res.status....` while your lambda get `responses`. where is `res` come from? – Omri Attiya Apr 06 '20 at 08:48
  • @OmriAttiya its in app.get('/', urlencodedParser, (req, res) => {} All the code I posted above is here. – 1x2x3x4x Apr 06 '20 at 09:03
  • @Farrukh Yeah, as I said I think that's the problem. But I'm not sure where or how should I .end() it? – 1x2x3x4x Apr 06 '20 at 09:04
  • Well for this case you don't need to call .end() because there is no buffer you are making to send the response plus @OmriAttiya is right about res.status it should be responses.status... – Farrukh Apr 06 '20 at 09:19
  • @Farrukh I've updated the post with a more comprehesive code, if that helps. I've changed to what you and OmriAttiya said. Still no success. – 1x2x3x4x Apr 06 '20 at 09:28

1 Answers1

0

You can not use send with end.

  • end() is used when you want to end the request and want to respond with no data.

  • send() is used to end the request and respond with some data.

You can found more about it here.

ziishaned
  • 4,944
  • 3
  • 25
  • 32
  • Thanks. I removed the .end() However I face the same problem. Even if the try is succesful and the sendCommandToDevice works, I still get as repsonse the catch. – 1x2x3x4x Apr 06 '20 at 09:15