在SQL Server存储过程运行完毕后,我尝试从node/express/tedious执行一个python脚本。
当用户单击submit时,将向node/express发送一个post请求:
const onSubmit = async (data) => {
await fetch('http://example.com:4001/foo/post/create', {
method: 'POST',
headers: authHeader(),
body: JSON.stringify({
fid: data.fid,
geomwkt: data.geomwkt,
srid: data.srid,
.
.
.
})
}).then(res => {
return res.text();
})
.then(data => console.log('Success:', data))
.catch(error => console.log('Error:', error))
history.push('/foo');
}它又通过router.post (express4/tedious)运行SQL Server存储过程:
router.post('/post/create', textParser, function (req, res) {
req.sql("exec create_alignment @align")
.param('align', req.body, TYPES.NVarChar)
.exec(res);
});所以现在我想在存储过程完成后执行一个python脚本:
router.post('/post/create', textParser, function (req, res) {
req.sql("exec create_alignment @align")
.param('align', req.body, TYPES.NVarChar)
.exec(res);
const abc = JSON.parse(req.body);
exec('python3 /home/ubuntu/scripts/runthis.py ' + param1 + ' ' + param2, (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(`stdout:\n${stdout}`);
});
});但是python脚本在存储过程完成之前运行。因此,我尝试将async/await添加到混搭中:
router.post('/post/create', textParser, async function (req, res) {
await req.sql("exec create_alignment @align")
.param('align', req.body, TYPES.NVarChar)
.exec(res);
const abc = JSON.parse(req.body);
await exec('python3 /home/ubuntu/scripts/runthis.py ' + param1 + ' ' + param2, (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(`stdout:\n${stdout}`);
});
});但这似乎并没有改变任何事情。在存储过程完全完成后,如何运行python脚本?
发布于 2021-01-11 10:08:28
根据我的理解,乏味的节点不支持ter async/await (但我对node非常陌生,所以可能最近事情发生了变化,我只是不知道)。您必须通过手动编码promise的结果来确保您希望在过程之后执行的代码。
我在这里专门写了一篇关于这方面的文章:
https://stackoverflow.com/questions/65106015
复制相似问题