Reputation: 21
I have this code on my server, but whenever it runs, it always gives me an error. When I go to the developer console in Chrome, It just says error, but has no further information to help me figure out why. I'm making this from a template and I have gone through the changes I made over and over, but I can't figure out why it stopped working. Any help is appreciated.
Here is the code:
// 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")
}
});
}
});
Here is the original code from the template that worked before I changed it:
// 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" });
}
});
}
});
Here is a link to the full project if it helps: https://glitch.com/edit/#!/beta2-droneswap?path=server.js%3A215%3A0
Thank you for your time, and once again, any help is appreciated.
Edit: There was another error happening on startup, but I fixed that and it still doesn't work. The current error happens when I try to add a listing. I'm pretty sure that it has something to do with the sql code around line 166.
Here is the current code where I think that the error is:
// 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");
}
}
);
}
});
Upvotes: 0
Views: 113
Reputation: 136
So I remixed your example and added more content to the console logs of errors, just so I can see where the error is happening better, and it looks like your database creation is not completing successfully. I think it's because you've commented out the content inside the db.run
call on line 165. If you don't want that code to run, you should include the db.run
call in the comment block as well, otherwise it fails to run and that's throwing that initial error. Hope this helps!
Upvotes: 1