Javascript 中的嵌套 SQL.promise() 回调

Nested SQL.promise() callback in Javascript

我想编写嵌套回调 JavaScript 以便在不同的 SQL 表中使用 INSERT 语句的后续 ID。我的代码如下所示:

db.promise().query(
     // create Place with auto-generated PlaceID
      `INSERT INTO Place (Street,PLZ) VALUES('${street}', ${plz})`
    ,
    (err,result) => { 
      db.promise().query( 
          // insert User using PlaceID
          `INSERT INTO User (PlaceID, Name, Created_On) VALUES(${result.insertID}, '${name}',current_timestamp())`
        ,
        (err,result) => { 
          // insert Account using UserID
          `INSERT INTO Account (UserID, Email, Password, Created_On) VALUES(${result.insertID}, '${email}', '${pwd}', current_timestamp())`
        }
      );
    }
);

但是,当尝试使用 Node.js 运行 时,它显示 Error: Callback function is not available with promise clients
您有检索以下插入的插入 ID 的解决方案吗?

我也试过 .then((result)=>{..}) 这似乎有效,但 result.InsertID 总是未定义?

// create place with auto-generated PlaceID
db.promise().query(`INSERT INTO Place (Street,PLZ) VALUES('${strasse}', ${plz})`)
              .then((err,result) => { 
                if(err)throw err;
                // insert User using PlaceID
                db.promise().query(
                    `INSERT INTO User (PlaceID, Name, Created_On) VALUES(${result.insertID}, '${name}',current_timestamp())`)
                  .then((err,result) => { 
                    if(err)throw err;
                    // insert Account using UserID
                    db.promise().query(`INSERT INTO Account (UserID, Email, Password, Created_On) VALUES(${result.insertID}, '${email}', '${pwd}', current_timestamp())`)
                  });
              }).catch((err)=>{console.log(err)});

您正在混合这两种风格。如果你想传递一个错误优先的回调,你不应该使用 promise() API.

// INCORRECT: If you're using promise(), you use .then,
// not an error-first callback.
db.promise().query(/* ... */,
    (err,result) => { 
      /* ... */
    }
);

// CORRECT: Delete the promise() to use an error-first callback.
db.query(/* ... */,
    (err,result) => { 
      /* ... */
    }
);

可以 使用 promises,但如果您这样做,则不会使用参数 (err, result)。传递给 then 的函数使用单个参数 result,而 err 将在链式 catch 中被处理 either有以下,或(很少)in a second function passed to then.

// INCORRECT: The function passed to `then` should never take two arguments.
db.promise().query(/* ... */)
  .then((err,result) => { 
    /* ... */
  }).catch((err)=>{console.log(err)});

// CORRECT: The function passed to `then` only takes a `result`.
// If there were an error in your outer query, it would call
// your `catch` handler.
db.promise().query(/* ... */)
  .then(result => { 
    /* ... */
  }).catch((err)=>{console.log(err)});

// ALSO CORRECT: The call to `then` can also receive an error handler,
// but it will only catch errors in your outer `query`. If your `then`
// handler throws any errors, this style of chaining won't catch them.
db.promise().query(/* ... */)
  .then(result => { 
    /* ... */
  }, err => {
    /* ... */
  });

原来它必须是 result.insertId 而不是 result.insertID 然后它不再是 return NULL..