通过nodejs与MS SQL Server进行交互

问题描述

对不起,如果这是一个非常基本的问题,但是我在这个问题上停留了一段时间,并且在节点上还很陌生。

我正在制作一个API,以将某些订单同步到数据库中。在插入之前,我们需要验证客户是否已经存在,并在必要时创建它。我在代码开头将变量no设置为0,然后如果客户存在,我们需要将其设置为另一个数字。

如果我在查询功能内部控制台日志,则可以访问新值,但在新值之外不可用。

app.post("/order",verifyJWT,(req,res,next) => {
app.use(bodyParser.json());
var request = new sql.Request();
var EncomendJson = req.body;
let no = 0;
var ndos = 10;

//console.log(EncomendJson.documento.contribuinte);

let contribuinte = EncomendJson.documento.contribuinte; 
if (contribuinte == '999999990') {
    //vamos buscar à base de dados buscar o numero de cliente
     no = 1;

}
else {
    request.query("select count(*) as numero from cl (NOLOCK) where ncont = '"+contribuinte+"' and pncont = 'PT'",function (err,row) {
        if (err) console.log(err);

            var clienteExiste  = row.recordset[0]["numero"]; 
            
            if (clienteExiste == 0) {
                // vamos fazer a criação do cliente
            } else {
                // vamos buscar o numero do cliente à base de dados
            
                 request.query("select no as no from cl (NOLOCK) where ncont = '"+contribuinte+"' and pncont = 'PT'")
                 .then( row1 => {
                     no = row1; 
                     

                    
                 })
            }
            console.log(row1);
            
        
   })
}


// Verifica o número de linhas do documento
var numero_linhas_documento = Object.keys(EncomendJson.documento.vendas).length;

res.json('done'); })

我在数据库查询中做错了吗?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...