Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Am I explaining this Node method correctly?

Am I explaining this function correctly? Am I using the right terms and logic?

Here's my method:

addOrder() {
❶ const db = getDb();
② return this.getCart()
  .then(products => {
	const order = {
	  items: products,
	  user: {
		_id: new ObjectId(this._id),
		name: this.name,
		email: this.email
	  }
	};
	❸ return db.collection("orders").insertOne(order);
  })
  ❹ .then(result => {
	this.cart = { items: [] };
	return db
	  .collection("users")
	  .updateOne(
		{ _id: new ObjectId(this._id) },
		{ $set: { cart: { items: [] } } }
	  );
  });
}

Open in new window


I want to be able to understand this, not just in terms of its logical flow, but also in the theory behind it so I can explain it to others...

Here's what I've got. Let me know if I've nailed it or I need to tweak my understanding.


① I'm establishing my database connection

② you'll need to return the result of the "then" block so your controller has a result to publish. That part of the Controller looks like this:

exports.getOrders = (req, res, next) => {
  req.user
    .getOrders()
    .then(orders => {
      res.render("shop/orders", {
        path: "/orders",
        pageTitle: "Your Orders",
        orders: orders
      });
    })
    .catch(err => console.log(err));
};

In addition, the result of the "getCart" is going to be all of the data that's coming from the "getCart" method which includes the productId and the quantity. You'll throw that in the "items" object and then populate the user object with the properties and values coming from the stored user id.

Like this...

"const order = { }" - when you see that, know that what's positioned between the two curly braces is an "object." An object, by definition, is a collection of properties and values, although you can have an object within an object as well. You see that here.

In this case, "items" is a property and "products" is your value, which in this example, just happens to be an array.

"user" is an object with the properties being "_id," "name" and "email" and the values being what's coming from the the properties and values that came from the constructor at the top of the class:

class User {
  constructor(username, email, cart, id) {
    this.name = username;
    this.email = email;
    this.cart = cart; // {items: []}
    this._id = id;
  }

③ the "return" that you used with ①  was for the sake of your Controller so it had something to display. This "return" is just a part of your typical Mongo insertion syntax.

④ this "then" block is happening in the case that the first "then" block was completed successfully. In this case what's happening is your emptying the items that correspond to this user's cart as well as the cart items that were documented in the "users" table.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Thanks, Julian!
You are welcome Bruce