Why do I get ReferenceError: insertId is not defined on my 4th MySQL Query in NodeJS?
Why do I get ReferenceError: insertId is not defined on my 4th MySQL Query in NodeJS?
为什么我能够从第一个 MySQL 查询中获取 insertId 而不是在第四个查询的代码中?查询有点复杂,但我不知道为什么第一次有效的东西以后在代码中不起作用;
var mysql = require("mysql");
var express = require("express");
var connection = require("../database")
var createAudiopost = function(req, res, next){
var title = req.body.title;
var userid = req.body.userid;
var opid = req.body.opid;
var tag1 = req.body.tag1;
connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query('INSERT INTO ?? (title,userid,opid) VALUES (?, ?, ? )', ['audioposts',title,userid,opid], function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
var audioname = userid + '-' + result.insertId + '.m4a';
var newid = result.insertId;
console.log("newid: " , newid );
connection.query('UPDATE ?? SET audioname=? WHERE audioid = ?', ['audioposts',audioname,newid], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
});
}
if (tag1) {
connection.query('SELECT tagid FROM tags WHERE tagname = ?', [tag1], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
});
}
if (result < 1) {
connection.query('INSERT INTO tags SET tagname = ?', [tag1], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log("lets see this wierd error", result);
const tagInsertId = result.insertId;
connection.query("INSERT INTO entitytag SET audioid = ?, tagid = ?, userid = ?", [insertId, tagInsertId, userId], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log('success!');
newid = result.insertId;
res.json({
"title" : title,
"userid" : userid,
"opid" : opid,
"insertid": newid
}); //resjson success
}); //commit
}); // insert entitytags
}); // insert tags
} // if row
else {
const tagid1 = result.tagid;
connection.query("INSERT INTO entitytag SET audioid = ?, tagid = ?, userid = ?", [insertId, tagid1, userId], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
}); //err
} //err
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log('success!');
res.json({
"title" : title,
"userid" : userid,
"opid" : opid,
"insertid": newid
}); //resjson success
}); //commit
}) // insert entitytag2
}
}); //select tagid
}//tag1
}); //update
}); //insert
}); //begin transaction
} //createaudiopost
module.exports = createAudiopost;
代码达到;
sql: "INSERT INTO tags SET tagname = 'dogmatism'"
但是之后我直接得到的错误是;
Parser.js:437
throw err; // Rethrow non-MySQL errors
^
ReferenceError: insertId is not defined
insertId
未定义但用于以下查询:
connection.query("INSERT INTO entitytag SET audioid = ?, tagid = ?, userid = ?", [insertId, tagid1, userId], function (error, result, fields)
根据查询中的用法,您似乎打算使用 newid
:
connection.query('UPDATE ?? SET audioname=? WHERE audioid = ?', ['audioposts',audioname,newid], function (error, result, fields)
在这里替换相同的内容,它应该可以工作。
connection.query("INSERT INTO entitytag SET audioid = ?, tagid = ?, userid = ?", [newid, tagid1, userId], function (error, result, fields)
如果没有创建一个名为 insertId 的变量并在使用前对其进行初始化。
为什么我能够从第一个 MySQL 查询中获取 insertId 而不是在第四个查询的代码中?查询有点复杂,但我不知道为什么第一次有效的东西以后在代码中不起作用;
var mysql = require("mysql");
var express = require("express");
var connection = require("../database")
var createAudiopost = function(req, res, next){
var title = req.body.title;
var userid = req.body.userid;
var opid = req.body.opid;
var tag1 = req.body.tag1;
connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query('INSERT INTO ?? (title,userid,opid) VALUES (?, ?, ? )', ['audioposts',title,userid,opid], function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
var audioname = userid + '-' + result.insertId + '.m4a';
var newid = result.insertId;
console.log("newid: " , newid );
connection.query('UPDATE ?? SET audioname=? WHERE audioid = ?', ['audioposts',audioname,newid], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
});
}
if (tag1) {
connection.query('SELECT tagid FROM tags WHERE tagname = ?', [tag1], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
});
}
if (result < 1) {
connection.query('INSERT INTO tags SET tagname = ?', [tag1], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log("lets see this wierd error", result);
const tagInsertId = result.insertId;
connection.query("INSERT INTO entitytag SET audioid = ?, tagid = ?, userid = ?", [insertId, tagInsertId, userId], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log('success!');
newid = result.insertId;
res.json({
"title" : title,
"userid" : userid,
"opid" : opid,
"insertid": newid
}); //resjson success
}); //commit
}); // insert entitytags
}); // insert tags
} // if row
else {
const tagid1 = result.tagid;
connection.query("INSERT INTO entitytag SET audioid = ?, tagid = ?, userid = ?", [insertId, tagid1, userId], function (error, result, fields) {
if (err) {
connection.rollback(function() {
throw err;
}); //err
} //err
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log('success!');
res.json({
"title" : title,
"userid" : userid,
"opid" : opid,
"insertid": newid
}); //resjson success
}); //commit
}) // insert entitytag2
}
}); //select tagid
}//tag1
}); //update
}); //insert
}); //begin transaction
} //createaudiopost
module.exports = createAudiopost;
代码达到;
sql: "INSERT INTO tags SET tagname = 'dogmatism'"
但是之后我直接得到的错误是;
Parser.js:437
throw err; // Rethrow non-MySQL errors
^
ReferenceError: insertId is not defined
insertId
未定义但用于以下查询:
connection.query("INSERT INTO entitytag SET audioid = ?, tagid = ?, userid = ?", [insertId, tagid1, userId], function (error, result, fields)
根据查询中的用法,您似乎打算使用 newid
:
connection.query('UPDATE ?? SET audioname=? WHERE audioid = ?', ['audioposts',audioname,newid], function (error, result, fields)
在这里替换相同的内容,它应该可以工作。
connection.query("INSERT INTO entitytag SET audioid = ?, tagid = ?, userid = ?", [newid, tagid1, userId], function (error, result, fields)
如果没有创建一个名为 insertId 的变量并在使用前对其进行初始化。