संपादित करें: ने थोड़ा और कोड जोड़ा।
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;
मेरे पास यह फ़ंक्शन है, जिसे मैं क्लाइंट आरंभ करने के बाद कॉल करता हूं: 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();
});
}
मेरी समस्या यह है कि sendCommandToDevice पूरी तरह से निष्पादित हो जाता है, हालांकि मुझे पकड़ त्रुटि मिलती है। जैसा कि मैं इसे समझता हूं, ऐसा इसलिए है क्योंकि .फिर में कनेक्शन समाप्त हो जाता है।
मैंने यह देखा है और यही मैंने कोशिश की है, हालांकि मैंने मुझे यकीन नहीं है कि मैं समझ रहा हूं कि क्या हो रहा है।
2 जवाब
आप send
का उपयोग end
के साथ नहीं कर सकते।
end()
का उपयोग तब किया जाता है जब आप अनुरोध समाप्त करना चाहते हैं और बिना डेटा के जवाब देना चाहते हैं।send()
का उपयोग अनुरोध को समाप्त करने और कुछ डेटा के साथ प्रतिक्रिया करने के लिए किया जाता है।
आप इसके बारे में यहां अधिक जानकारी प्राप्त कर सकते हैं।
आपकी विधि sendCom
को वह responses
लौटाना चाहिए जो आपको .then()
में मिलता है और साथ ही end()
को कॉल नहीं करना चाहिए:
// your code...
return client
.sendCommandToDevice(request)
.then(responses => responses.status(200).send(JSON.stringify({ data: OK })))
.catch(err => console.error(err)); // do something with err
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
node.js
Node.js एक घटना-आधारित, गैर-अवरोधक, अतुल्यकालिक I / O रनटाइम है जो Google के V8 जावास्क्रिप्ट इंजन और libuv लाइब्रेरी का उपयोग करता है। इसका उपयोग उन अनुप्रयोगों को विकसित करने के लिए किया जाता है जो क्लाइंट पर और साथ ही सर्वर साइड पर जावास्क्रिप्ट को चलाने की क्षमता का भारी उपयोग करते हैं और इसलिए कोड के पुन: प्रयोज्य और संदर्भ स्विचिंग की कमी से लाभान्वित होते हैं।
res.status....
का उपयोग कर रहे हैं जबकि आपके लैम्ब्डा कोresponses
मिलता है।res
कहां से आया है?