node.js 中的子查询有时有效,有时无效

Subquery in node.js sometimes works and sometimes not

我创建了一个 node.js 查询,有时它工作正常,它是一个俱乐部的插入,然后在第二个查询中插入球员,有一个子查询获取最后插入的 id在第一个查询中自动增加 id ,执行后我注意到有时玩家会插入 最后插入的俱乐部 ID 就像它们应该的那样,但有时它们会被插入,但不是最后插入的 ID,而是之前的 ID!!!!!!

提示: 我在回调的 return 中看到一切正常,我看到了 insertId回调值是正确的值,但是当我不工作时,我在回调的结果中看到 insertId = 0

问题示例: 假设最后插入的 club id 是 15 ,当问题发生时 我发现插入的球员插入了 clubid 14 !!!!!!!!!!!!!!!他们应该是15岁 但最奇怪的是,我发现有时它们会以正确的值正确插入!!!!

它就像一个随机的东西,有时它选择工作,有时不工作!

这里是postman中的json:

{
    "userid": 41,
    "name": "fc dhya club",
    "price": 11,
    "players": [
        {
            "id":1,
            "firstname":"aaaaaaa",
            "lastname":"ee",
            "position":"df",
            "price":12.1,
            "appearences":2,
            "goals":1,
            "assists":1,
            "cleansheets":1,
            "redcards":1,
            "yellowcards":1,
            "image":"qq"
        },
        {
            "id":2,
            "firstname":"aabbbbbbbbbbb",
            "lastname":"ee",
            "position":"df",
            "price":12.1,
            "appearences":2,
            "goals":1,
            "assists":1,
            "cleansheets":1,
            "redcards":1,
            "yellowcards":1,
            "image":"qq"
        }
    ]
}

这里是node.js代码

const { json } = require("express");
const mysql = require('mysql2');
const pool = require("../../config/database");

module.exports = {
    create: (data, callback) => {
        var myArray = new Array();

        data.players.forEach((player) => {
            var playerModel = {
                id : player.id,
                firstname : player.firstname,
                lastname : player.lastname,
                position : player.position,
                price : player.price,
                appearences : player.appearences,
                goals : player.goals,
                assists : player.assists,
                cleansheets : player.cleansheets,
                redcards : player.redcards,
                yellowcards : player.yellowcards,
                image : player.image
            };

            myArray.push(playerModel);
        });

        var id;

        pool.query(
            'insert into club(userid,name,price) values(?,?,?)',
            [
                data.userid,
                data.name,
                data.price
            ],
            (error, result) => {
                if (error) {
                    callback(error);
                }

                return callback(null, result);
            },
        );

        for(var item of myArray) {
            pool.query(
                'insert into players(id,firstname,lastname,position,price,appearences,goals,assists,cleansheets,redcards,yellowcards,image,clubid Where clubid = ( SELECT id from club where id > ? ORDER BY id DESC limit 1  )) values (?,?,?,?,?,?,?,?,?,?,?,?, ( SELECT id from club where id > ? ORDER BY id DESC limit 1  ) )',
                [ 
                    item.id,
                    item.firstname,
                    item.lastname,
                    item.position,
                    item.price,
                    item.appearences,
                    item.goals,
                    item.assists,
                    item.cleansheets,
                    item.redcards,
                    item.yellowcards,
                    item.image,
                    0
                ],
                (error, results, fields) => {
                    if(error){
                        callback(error);
                    }

                    return callback(null, results);
                },
            );
        }
    },
};
  

这里是邮递员在查询不能正常工作时的屏幕截图

此处插入正常时

原因是您没有考虑 pool.query() 的异步性质。当您进行第一个查询时,即插入球杆,您无需等待它完成,您只需开始插入期望插入正确球杆的球员。有时俱乐部可能已被插入,有时不会,因为它是异步的。您需要做的是将第二个循环移动到第一个 pool.query() 的回调函数中。这样,第二个循环只有在确定插入球杆后才是 运行。检查此代码。

pool.query(
    'insert into club(userid,name,price) values(?,?,?)',
    [
        data.userid,
        data.name,
        data.price
    ],
    (error, result) => {
        if (error) {
            return callback(error);
        }
        
        for(var item of myArray) {
            pool.query(
                'insert into players(id,firstname,lastname,position,price,appearences,goals,assists,cleansheets,redcards,yellowcards,image,clubid Where clubid = ( SELECT id from club where id > ? ORDER BY id DESC limit 1  )) values (?,?,?,?,?,?,?,?,?,?,?,?, ( SELECT id from club where id > ? ORDER BY id DESC limit 1  ) )',
                 ...
                 ...
    },
);