首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在从node/express/tedious执行python脚本之前,请等待存储过程完全完成

在从node/express/tedious执行python脚本之前,请等待存储过程完全完成
EN

Stack Overflow用户
提问于 2020-12-02 18:11:26
回答 1查看 169关注 0票数 0

在SQL Server存储过程运行完毕后,我尝试从node/express/tedious执行一个python脚本。

当用户单击submit时,将向node/express发送一个post请求:

代码语言:javascript
复制
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存储过程:

代码语言:javascript
复制
router.post('/post/create', textParser, function (req, res) {

    req.sql("exec create_alignment @align")
        .param('align', req.body, TYPES.NVarChar)
        .exec(res);

});

所以现在我想在存储过程完成后执行一个python脚本:

代码语言:javascript
复制
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添加到混搭中:

代码语言:javascript
复制
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脚本?

EN

回答 1

Stack Overflow用户

发布于 2021-01-11 10:08:28

根据我的理解,乏味的节点不支持ter async/await (但我对node非常陌生,所以可能最近事情发生了变化,我只是不知道)。您必须通过手动编码promise的结果来确保您希望在过程之后执行的代码。

我在这里专门写了一篇关于这方面的文章:

Promises, Node, Tedious, Azure SQL. Oh My!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65106015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档