【问题标题】:Nested SQL.promise() callback in JavascriptJavascript 中的嵌套 SQL.promise() 回调
【发布时间】:2021-10-26 17:58:32
【问题描述】:

我想编写一个嵌套回调 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
您是否有解决方案来检索以下 INSERTS 的插入 ID?

我也尝试过.then((result)=>{..}),这似乎可行,但结果。插入ID 始终未定义?

// 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)});

【问题讨论】:

  • 你为什么用db.promise().query()而不是db.query()
  • 您使用.then((result) => ...) 处理承诺的结果。
  • @Barmar 当我在没有 promise() 的情况下执行此操作时,它表示 HTTP 标头是在客户端之前设置的。我尝试使用 .then((result) =>..) 但这给了我一个 nhandledPromiseRejectionWarning

标签: javascript sql node.js promise callback


【解决方案1】:

你混合了这两种风格。如果您想传递错误优先回调,则不应使用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) => { 
      /* ... */
    }
);

可以使用promise,但是如果你这样做了,那么你就不会使用参数(err, result)。传递给then 的函数使用单个参数result,而err 将在链接的catch 中处理或者,如下所示,或者(不经常)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 返回 NULL 的错误实际上是因为它必须是 result.insertId 没有大写 D。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多