Nodejs SQL查询正在更新不正确的行

hgqdbh6s  于 2022-12-26  发布在  Node.js
关注(0)|答案(1)|浏览(140)

在我的API中,我有一个POST请求,在其中我将文件url添加到一行,使用我想要更新的行的Id来指定所述文件url应该去哪里。2下面是相关代码:

router.post('/expert-assessment/contract/upload',verifyToken, async(req,res) => {
    const token = req.headers['token'];
    var id = 0;
    var instanceId = 0;
    var name = [];
    var type = [];
    jwt.verify(token, process.env.REACT_APP_ReadToken,function(err, decoded) {
        if(err !== null)
        {
            res.send(false);
        }
        else
        {
            id = decoded.Id;
            name = decoded.DocumentName;
            email= decoded.Email;
            type = decoded.Type;
            instanceId = decoded.InstanceId
        }
      });
    const blobServiceClient = new BlobServiceClient(
      `https://${process.env.azContainer}.blob.core.windows.net${process.env.sas}`
    );
    var fileUrl = "";
    for(var i = 0; i < name.length; i++)
    {
        const containerClient = blobServiceClient.getContainerClient("expert-assessment-contracts");
        const fileName = name[i];
        
        const blockBlobClient = containerClient.getBlockBlobClient(fileName);

        var stream = "";
        if(name.length === 1)
        {
           stream = getStream(req.files.file.data);
        }
        else
        {
           stream = getStream(req.files.file[i].data);
        }
        const uploadBlobResponse = await blockBlobClient.uploadStream(stream, 1024 * 1024 * 4, 20,
        { blobHTTPHeaders: { blobContentType: type[i] } });
        if(i === 0)
        {
            fileUrl = blockBlobClient.url;
        }
    }

        var connection = new Connection(config);   
        connection.on('connect', function(err) {  
            if(err)
            {
                res.send(err);     
            }
            else
            {                
                request = new Request(`update dbo.ExpertAssessmentJourney set Contract='${fileUrl}', NumberOfDocuments='${name.length}' where Id=${instanceId}`, function(err) {  
                if (err) 
                {  
                    res.send(false);  
                } 
                else
                {
                    res.send(true);  
                } 
                });  
                console.log(request)
                connection.execSql(request);                  
            }
        });
});

但是,当我在执行请求/查询之后检查数据库中的其他行时,它们似乎都用最近示例中上载的相同文件URL更新。
通过记录变量并在本地进行测试,我已经确定ID是正确的,因为它是我想要更改的行的ID,并且我已经确定正在执行正确的请求。
值得一提的是,我正在编辑遗留代码;以前的查询是根据用户ID更新的,我可以理解为什么在生产环境中使用错误的代码时仍然会这样做,但我在本地测试时,它似乎仍然在执行错误的查询?先谢谢您

km0tfn4u

km0tfn4u1#

此循环:

for(var i = 0; i < name.length; i++)
    {
        const containerClient = blobServiceClient.getContainerClient("expert-assessment-contracts");
        const fileName = name[i];
        
        const blockBlobClient = containerClient.getBlockBlobClient(fileName);

        var stream = "";
        if(name.length === 1)
        {
           stream = getStream(req.files.file.data);
        }
        else
        {
           stream = getStream(req.files.file[i].data);
        }
        const uploadBlobResponse = await blockBlobClient.uploadStream(stream, 1024 * 1024 * 4, 20,
        { blobHTTPHeaders: { blobContentType: type[i] } });
        if(i === 0)
        {
            fileUrl = blockBlobClient.url;
        }
    }

在运行以下命令时完成:

var connection = new Connection(config);   
        connection.on('connect', function(err) {  
            if(err)
            {
                res.send(err);     
            }
            else
            {                
                request = new Request(`update dbo.ExpertAssessmentJourney set Contract='${fileUrl}', NumberOfDocuments='${name.length}' where Id=${instanceId}`, function(err) {  
                if (err) 
                {  
                    res.send(false);  
                } 
                else
                {
                    res.send(true);  
                } 
                });  
                console.log(request)
                connection.execSql(request);                  
            }
        });

所以你只需要更新上面变量赋值的最后一个结果。

相关问题