Javascript 我的sqlite项目在添加到表时不断抛出错误
我的服务器上有这段代码,但每当它运行时,总是给我一个错误。当我在Chrome中进入开发者控制台时,它只显示错误,但没有进一步的信息来帮助我找出原因。我是从一个模板做的,我已经经历了一遍又一遍的修改,但是我不明白为什么它停止工作了。感谢您的帮助 代码如下:Javascript 我的sqlite项目在添加到表时不断抛出错误,javascript,mysql,sql,sqlite,Javascript,Mysql,Sql,Sqlite,我的服务器上有这段代码,但每当它运行时,总是给我一个错误。当我在Chrome中进入开发者控制台时,它只显示错误,但没有进一步的信息来帮助我找出原因。我是从一个模板做的,我已经经历了一遍又一遍的修改,但是我不明白为什么它停止工作了。感谢您的帮助 代码如下: // endpoint to add a dream to the database app.post("/addDream", (request, response) => { console.log(`add
// endpoint to add a dream to the database
app.post("/addDream", (request, response) => {
console.log(`add to dreams ${request.body.name} ${request.body.price} ${request.body.email} ${request.body.description} ${request.body.photoID} ${request.body.date}`);
// DISALLOW_WRITE is an ENV variable that gets reset for new projects
// so they can write to the database
if (!process.env.DISALLOW_WRITE) {
const cleansedName = cleanseString(request.body.name);
const cleansedPrice = cleanseString(request.body.name);
const cleansedEmail = cleanseString(request.body.name);
const cleansedState = cleanseString(request.body.name);
const cleansedDescription = cleanseString(request.body.name);
const cleansedphotoID = cleanseString(request.body.name);
const cleansedDate = cleanseString(request.body.name);
db.run("INSERT INTO Dreams (name, price, email, description, photoID, date) VALUES (cleansedName, cleansedPrice, cleansedEmail, cleansedState, cleansedDescription, cleansedphotoID, cleansedDate);", error => {
if (error) {
response.send({ message: "error!" });
console.log("error")
} else {
response.send({ message: "success" });
console.log("success")
}
});
}
});
以下是我更改模板之前工作的模板的原始代码:
// endpoint to add a dream to the database
app.post("/addDream", (request, response) => {
console.log(`add to dreams ${request.body.dream}`);
// DISALLOW_WRITE is an ENV variable that gets reset for new projects
// so they can write to the database
if (!process.env.DISALLOW_WRITE) {
const cleansedDream = cleanseString(request.body.dream);
db.run(`INSERT INTO Dreams (dream) VALUES (?)`, cleansedDream, error => {
if (error) {
response.send({ message: "error!" });
} else {
response.send({ message: "success" });
}
});
}
});
以下是完整项目的链接(如果有帮助):
感谢您抽出时间,再次感谢您的帮助
编辑:启动时发生了另一个错误,但我修复了它,它仍然无法工作。当前错误发生在我尝试添加列表时。我很确定这与第166行附近的sql代码有关 以下是我认为错误所在的当前代码:
// endpoint to add a dream to the database
app.post("/addDream", (request, response) => {
console.log(
`add to dreams ${request.body.name}, ${request.body.price}, ${request.body.email}, ${request.body.state}, ${request.body.description}, ${request.body.photoID}, ${request.body.date}`
);
// DISALLOW_WRITE is an ENV variable that gets reset for new projects
// so they can write to the database
if (!process.env.DISALLOW_WRITE) {
const cleansedName = cleanseString(request.body.name);
const cleansedPrice = cleanseString(request.body.price);
const cleansedEmail = cleanseString(request.body.email);
const cleansedState = cleanseString(request.body.state);
const cleansedDescription = cleanseString(request.body.description);
const cleansedphotoID = cleanseString(request.body.photoID);
const cleansedDate = cleanseString(request.body.date);
db.run(
`INSERT INTO Listings (name, price, email, state, photoID, date) VALUES (cleansedName, cleansedPrice, cleansedEmail, cleansedState, cleansedDescription, cleansedphotoID, cleansedDate)`,
error => {
if (error) {
response.send({ message: "error!" });
console.log("error");
} else {
response.send({ message: "success" });
console.log("success");
}
}
);
}
});
因此,我重新混合了您的示例,并在控制台错误日志中添加了更多内容,以便更好地查看错误发生的位置,并且看起来您的数据库创建没有成功完成。我认为这是因为您注释掉了
db.run
call第165行中的内容。如果您不希望该代码运行,那么还应该在注释块中包含db.run
调用,否则它将无法运行,这将引发初始错误。希望这有帮助 查看如何在工作代码中执行值(?)
,对已编辑的代码重复该操作,执行值(cleansedName,
将不起作用(请注意,所有值都设置为请求.body.name
),不使用参数化查询也是不安全的。我将其更改为该格式,但仍然不起作用。这确实是一个问题,谢谢您指出,但在我更改后,它仍然显示相同的错误。您是否介意再看一次该项目,看看是否可以找到其他内容?我觉得错误可能是形式错误第204行的注释,但我不知道如何更改。查看应用程序的控制台日志,您会看到文本与第176行匹配。您试图对不存在的行执行操作,因为您注释掉了上面添加这些行的块。我已取消了这些行的注释,但仍然显示错误。我认为这与语法有关第166行的代码,但我不知道这是怎么回事。