如何解决通过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 举报,一经查实,本站将立刻删除。