Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not merge] try to solve postproduct with promise #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/controllers/addProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@ exports.post = (req, res) => {
delete product.rating;
delete product.comment;

// assign rating and comment to an object for passing to submitRating.js
// below I tried to isolate the problem and figured that this query might not do what it needs to do.
//The id always comes back as an empty array even with a productname that def. exists in the database
// let prodName = "Tofurky Feast";
// prodName = "'" + prodName + "'";
// console.log("prodname + quotes", prodName);
// getProductByName(prodName).then((id) => {
// console.log(id);
// }
// )

// Post the product & rating
postProduct(product).then(id => {
postProduct(product).then(() => {
let prodName = product.product_name;
prodName = "'" + prodName + "'";
console.log("prodname + quotes", prodName);
getProductByName(prodName);
}).then(id => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing is being returned by this arrow function. .then() is only a function of a promise as such you would need to have this arrow function return a promise explicitly by creating it directly:

.then() => {
  return new Promise ((resolve, reject) => {
   ... rest of code

but there is no need as the getProductsByName(prodName) call is returning connection.query (which is this essentially http://vitaly-t.github.io/pg-promise/Database.html#query) and so is returning a promise. So you can just return that on line 28
return getProductByName(prodName) so that the promise returned by connection.query gets returned by that and it can be used in the .then() on line 29

const rating_comment = {
rating: rating,
comment: comment
Expand Down
1 change: 1 addition & 0 deletions src/model/queries/getProductByName.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const connection = require("../database/db_connection");

module.exports = product_name => {
console.log("prodname in sql", product_name);
return connection.query("SELECT id FROM products WHERE product_name = $1", [
product_name
]);
Expand Down