[英]How do I send the output of a function to a client in NodeJS?
const express = require('express'); const { exec } = require('child_process') var currentJobs; function getCurrentJobs() { exec("lpstat", (error, stdout, stderr) => { if (error) { optimisedOutput = "An error occurred : " + `${error}`; } else if (stderr) { optimisedOutput = "An error occurred : " + `${stderr}`; } currentJobs = `${stdout}` console.log(currentJobs) return currentJobs; }) } app.get('/currentJobs', (req, res) => { currentJobs = getCurrentJobs() currentJobs = JSON.stringify(currentJobs) res.status(200).send(currentJobs) console.log(currentJobs); })
But, the variable currentJobs
does not update.但是,变量
currentJobs
不会更新。 Instead, I get an undefined error and Postman does not show any outputs.相反,我收到一个未定义的错误并且 Postman 没有显示任何输出。 It only shows the status code.
它只显示状态代码。
Please tell me what I'm doing wrong here.请告诉我我在这里做错了什么。
Edit: This will all be running on a Raspberry Pi 3 connected to the printer which is why I'm using CUPS.编辑:这一切都将在连接到打印机的 Raspberry Pi 3 上运行,这就是我使用 CUPS 的原因。
Your using exec as async execution, you either gonna need to change it to sync(prefer not to, for performance) or wrap it with promise, Ill show you the promise solution.您使用 exec 作为异步执行,您要么需要将其更改为同步(出于性能考虑,最好不要这样做)或用承诺包装它,我将向您展示承诺解决方案。
const express = require('express'); const { exec } = require('child_process') var currentJobs; async function getCurrentJobs() { return new Promise((resolve) => { exec("lpstat", (error, stdout, stderr) => { if (error) { optimisedOutput = "An error occurred : " + `${error}`; } else if (stderr) { optimisedOutput = "An error occurred : " + `${stderr}`; } currentJobs = `${stdout}` console.log(currentJobs) resolve(currentJobs); }) }) } app.get('/currentJobs', async (req, res) => { currentJobs = await getCurrentJobs() currentJobs = JSON.stringify(currentJobs) res.status(200).send(currentJobs) console.log(currentJobs); })
I think it will be better if you just promisify the function and use it as async/await as it will be much more readable and you are making a asynchronous call and expecting the behaviour of the code in a synchronous manner我认为如果您只是承诺函数并将其用作 async/await 会更好,因为它将更具可读性并且您正在进行异步调用并期望以同步方式的代码行为
const express = require("express");
const { exec } = require("child_process");
const app = express();
app.listen(9999, () => {
console.log("started");
});
function getCurrentJobs() {
return new Promise((resolve, rejects) => {
exec("lpstat", (error, stdout, stderr) => {
if (error) {
console.log("An error occurred : " + `${error}`);
rejects(error);
} else if (stderr) {
console.log("An error occurred : " + `${stderr}`);
rejects(stderr);
}
const currentJobs = `${stdout}`;
console.log(currentJobs);
resolve(currentJobs);
});
});
}
app.get("/currentJobs", async (req, res) => {
const currentJobs = await getCurrentJobs();
res.status(200).send(currentJobs);
console.log(currentJobs);
});
Try using this, it will help you.尝试使用这个,它会帮助你。
You'll have to return a promise otherwise getCurrentJobs() is not returning anything您必须返回一个承诺,否则 getCurrentJobs() 不会返回任何内容
const express = require("express"); const { exec } = require("child_process"); function getCurrentJobs() { return new Promise((resolve, rejects) => { exec("lpstat", (error, stdout, stderr) => { if (error) { console.log("An error occurred : " + `${error}`); rejects(error); } else if (stderr) { console.log("An error occurred : " + `${stderr}`); rejects(stderr); } const currentJobs = `${stdout}`; console.log(currentJobs); resolve(currentJobs); }); }); } app.get("/currentJobs", async (req, res) => { try { const currentJobs = await getCurrentJobs(); console.log(currentJobs); } catch(err) { res.status(500).send(err); } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.