首页 > 解决方案 > TypeError:将循环结构转换为 JSON 以获取 shell 脚本结果

问题描述

             const express = require('express');
             const app = express();
             const bodyParser = require('body-parser');
             const shell = require('shelljs');
             var sys = require('sys')
             var exec = require('child_process').exec;

             app.get('/api/v1/script', (request, response) =>{
                //console.log("HERE")
                var extra = "172.30.82.147 0000023FC160 qam"
                const ls = exec('source /root/chat-box/ip.sh ' + extra, 
                      (error, stdout, stderr) => {
                           console.log(`${stdout}`);
                           console.log(`${stderr}`);
                      if (error !== null) {
                            console.log(`exec error: ${error}`);
                      }
                      });
                console.log("$IP")
                const messages = process.env
                response.json({ ls });

                });

这段代码应该给我一个 IP,但我从中得到的只是

TypeError:在 ServerResponse.json (/root/chat-box/node_modules /express/lib/response.js:260:14) at app.get (/root/chat-box/server.js:31:12) at Layer.handle [as handle_request] (/root/chat-box/node_modules /express/lib/router/layer.js:95:5) 在下一个 (/root/chat-box/node_modules/express/lib/router/route.js:137:13) 在 Route.dispatch (/root/chat -box/node_modules/express/lib/router/route.js:112:3) 在 Layer.handle [as handle_request] (/root/chat-box/node_modules/express/lib/router/layer.js:95:5 ) 在 /root/chat-box/node_modules/express/lib/router/index.js:281:22 在 Function.process_params (/root/chat-box/node_modules/express/lib/router/index.js:335: 12)

但我也无法打印任何东西来看看出了什么问题

标签: javascriptnode.jsbash

解决方案


推荐阅读