会社のデータをクエリして表示するためのアプリケーションを MEAN スタックに構築しました。彼らは内部で Power Query を使用しているため、クエリの結果を TSV などの Excel 互換形式にエクスポートする API エンドポイントを設計しようとしています。これは、mongoexport コマンド ライン ユーティリティを使用して簡単に実行できますが、Node API を介してこのユーティリティの使用を自動化するのに苦労しています。このチュートリアルによると、これに対する私の現在の試みは次のとおりです。
var child = require('child-process');
var exec = child.exec();
module.exports.tsvData = function (query) {
//Atempted child process manipulation to use mongoexport CLI utility in this endpoint.
var command = 'echo "ANDY"'
child = exec(command, function (error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
};
これにより、次のエラーが返されます。
TypeError: object is not a function
at module.exports.tsvData (C:\Users\awimley\pstat\app_api\controllers\main.js:367:13)
at callbacks (C:\Users\awimley\pstat\node_modules\express\lib\router\index.js:164:37)
at param (C:\Users\awimley\pstat\node_modules\express\lib\router\index.js:138:11)
at pass (C:\Users\awimley\pstat\node_modules\express\lib\router\index.js:145:5)
at Router._dispatch (C:\Users\awimley\pstat\node_modules\express\lib\router\index.js:173:5)
これは child= 行にあります。これは、チュートリアルが古くなっているためではないかと疑ったため、子プロセス ドキュメントの構文を使用してみました。私は試した:
child = child_process.spawn('ls', {
stdio: [
0, // use parents stdin for child
'pipe', // pipe child's stdout to parent
fs.openSync('err.out', 'w') // direct child's stderr to a file
]
});
そして、次のエラーを返します。
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:1011:11)
at Process.ChildProcess._handle.onexit (child_process.js:802:34)
19 Oct 09:18:44 - [nodemon] app crashed - waiting for file changes before starting...
Node.js API からコマンド ライン ユーティリティを起動する際の支援は素晴らしいものです。