【问题标题】:multiple inserts node.js多个插入 node.js
【发布时间】:2021-07-18 10:37:21
【问题描述】:

我是 node.js 的新手, 我在我的 sql 数据库中有 2 个表,一个名为 club 的表和一个名为 player 的表,它们通过一对多关系连接,所以我在 Flutter 中创建了一个帖子查询,我想从我的查询中发送俱乐部的名称, 俱乐部的价格将它们插入到第一个表球队,并在同一查询中发送球队的选定球员列表并执行循环以将他们插入第二个表球员并插入俱乐部的 id 为每个玩家,但我发现我自己不知道我应该在 Flutter 或 Node.js 中正确的是什么,我正在尝试为我尝试过的内容找到正确的语法,执行后我在 node.js 中有一个错误说

unexpected end of json input because probably of players(that is a table and not a field in table club)

这是我尝试过的:

create:(data,callback)=>{
    pool.query(
      insert into club(userid,name,price) values(?,?,?),

      [
        data.userid,
        data.name,
        data.price,

      ],
      (error,results,fields)=>{
        if(error){
          callback(error);
        }
        return callback(null,results);
      }
    );
    for(item in data.players){
    pool.query(

      `insert into players(id,firstname,lastname,position,price,appearences,goals,
        assists,cleansheets,redcards,yellowcards,image,clubid) values(?,?,?,?,?,?,?,?,?,?,?,?,?)`,

      [
        data.players.id,
        data.players.firstname,
        data.players.lastname,
        data.players.position,
        data.players.price,
        data.players.appearences,
        data.players.goals,
        data.players.assists,
        data.players.cleansheets,
        data.players.redcards,
        data.players.yellowcards,
        data.players.image,
        data.players.clubid,
        

      ],
      (error,results,fields)=>{
        if(error){
          callback(error);
        }
        return callback(null,results);
      }
    );
  }
  },

我的数据库如下所示:

餐桌俱乐部:id userid

桌球员:id 名字 姓氏 位置 价格 出场次数 进球 助攻 不失球 红牌 黄牌 图片 clubid

clubid 是 table club 的外键 我知道它应该是最后插入的 id 但我不知道如何做正确的语法

任何帮助将不胜感激

【问题讨论】:

    标签: javascript mysql node.js json


    【解决方案1】:

    这里有两个问题。

    首先,我猜,您将在第二次 INSERT 中使用的唯一 clubid 是在您执行第一次 INSERT 时由 MySQL 的自动增量功能生成的。您可以在result.insertId 中的查询的回调中获取它。然后将其传递给第二个查询。

    其次,您在这里没有正确处理 MySQL 查询的异步特性。在您知道第一个 INSERT 完成之前,您不能执行第二个 INSERT。因此,您必须在第一个查询的回调中执行第二个 INSERT。

    或者更好的是使用mysql2 packageasync / await。像这样的东西(未调试)。

    create: async (data,callback) => {                               /* async!   */
        const myPool = pool.promise()                                /* promise! */
        const {results, fields} = await myPool.query(                /* await!   */
                'insert into club(userid,name,price) values(?,?,?)',
                [ data.userid, data.name, data.price] )
        const clubid = results.insertId
        for(item in data.players) {
            const {results, fields} = await myPool.query(            /* await!   */
               `insert into players
                   (id, firstname, lastname, position, 
                    price, appearences, goals, assists, 
                     cleansheets, redcards, yellowcards, 
                     image, clubid)
                  values(?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
                [   /* NOTE use   item.whatever   not   data.players.whatever,
                     * because data.players is an array of objects  */
                    item.id, item.firstname, item.lastname, item.position, 
                    item.price, item.appearences, item.goals, item.assists,
                    item.cleansheets, item.redcards, item.yellowcards,
                    item.image,
                    clubid ]   /* NOTE not item.clubid ! */
                   )
           } /* end for (item in data.players) */
        }
    }
    

    专业提示:花每一秒的时间来弄清楚async / await 和相关的Promises API 都是值得的。如果你不这样做,你就会陷入所谓的“回调地狱”。上面写着"abandon every hope, ye who enter here."

    【讨论】:

    • 这个函数会识别data.players吗?
    • 在任何表中没有字段叫做 player ,players 是 chid 表
    • 如果是的话,你能告诉我邮递员的身体应该怎么做?
    • 使用调试器。逐行遍历代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    相关资源
    最近更新 更多