簡體   English   中英

我的 nodejs api 混合了客戶端同時請求的 api 請求的代碼

[英]My nodejs api mixes the codes of simultaneous api requests by clients

我有一個 nodejs 服務器文件,其中包含如下所示的 api 來更新配置文件圖片。

app.post('/updateProfilePic', async(req, res) => {
try {
    if (VerifyAPIKey(req.query.key)) {
        let userdata = users.find(e => e.Id == req.query.socketId);
        if (userdata) {
            for (var a = 0; a < users.length; a++) {
                const b = a;
                if (users[a].IsAuthenticated) {
                    if (req.query.pub) {
                        cloudinary.uploader.destroy(req.query.pub, {resource_type: 'image'}, function(err, res) {
                            // console.log(err, res);
                        });
                    }
                    cloudinary.uploader.upload(req.files.profilePic.tempFilePath, {resource_type: 'image', folder: 'members', eager: [
                        {width: 25, height: 25, g: 'face', radius: "max", crop: 'fill', format: "png"},
                        {width: 50, height: 50, g: 'face', radius: "max", crop: 'fill', format: "png"},
                        {width: 100, height: 100, g: 'face', radius: "max", crop: 'fill', format: "png"},
                        {width: 250, height: 250, g: 'face', radius: "max", crop: 'fill', format: "png"},
                        {width: 500, height: 500, g: 'face', crop: 'fill'},
                    ]}, function(err,response) {
                        if (err) {
                            console.log(err);
                        }
                        if (response) {
                            const logo = userModel.findOneAndUpdate({
                                _id: users[b]._id,
                            },{
                                PictureUrl: response
                            }, (err, result) => {
                                data.status = 200;
                                data.message = "Your Profile Picture has been updated!"
                                res.status(200).send(data);
                            })
                        }
                    });
                }
            }
        }   else    {
            data.status = 404;
            data.message = "Invalid User!";
            res.status(200).send(data);
        }
    }   else    {
        res.json('Unauthorized request!');
    }
}   catch(err) {
    res.status(400).send(err.message);
}
})

下面給出了 VerifyAPIKey function

function VerifyAPIKey(key) {
   var a = users.find(e=> e.API_KEY == key);
   console.log(a)
   fs.appendFile('./data/apiRequests.txt', JSON.stringify(a) + "\r\n", function (err) {
       if (err) throw err;
   });
   return Boolean(a);
}

用戶數據的格式如下所示

{
   Id: 'FjWs0GZ4MkE_GCmKAAAD',
   Ip: '::1',
   API_KEY: '590c3789-e807-431b-bfdb-e20b6649e553',
   HOST: undefined,
   IsAuthenticated: false
}

問題是當前代碼導致來自 cloudinary 的響應數據在同時請求之間混淆。 我已經用兩個同時請求對其進行了測試。 在兩個雲響應中,以先到者為准,將作為響應發回給比兩者晚調用 api 的用戶。 調用 api 的用戶首先得到一個錯誤,即在發送后無法設置標頭。

我已經嘗試尋找解決方案,但沒有找到任何解決方案。 有人可以幫忙嗎?

data是如何啟動的? 數據似乎不是線程安全的,並且在您的異步流之外定義。 您可能希望從那里開始並確保data是線程安全的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM