Skip to content

Instantly share code, notes, and snippets.

@PistachioPony
PistachioPony / Passing info function to function
Last active August 29, 2015 13:56
How to pass information from function to function
// Once you get to next(); it moves on to the next function and lets go of whatever information was captured there.
// for instance mariaItem finds the item with that particular _id and when it hits the next(); that information is gone...
// UNLESS
// You add a req.contents = {}; with that you are adding to the request hash a contents key and {} value.
// It always will pass the req, res, option for next.
// Next you add a result key with a result value. (The result value holds the the work of findOne).
// So that information is now stored in the res hash and when the computer goes to next function it takes that info with it.
var mariaItem = function (req, res, next) {
Item.findOne({ _id: "5099803df3f4948bd2f98391"}, function (err, result){
@PistachioPony
PistachioPony / How to get info from the req you added to.
Last active August 29, 2015 13:56
Get information from the req.contents including from an Array
//Pull out the item with that id add it to the req.contents hash then go to next function...
var mariaItem = function (req, res, next) {
Item.findOne({ _id: "5099803df3f4948bd2f98391"}, function (err, result){
//res.json(result);
//var firstOne = res.json(result);
//{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
//Group.find({_id: {$in: req.contents.item.groups}}, function (err, groups) {
req.contents = {};
req.contents.result = result;
// SO here I have gone from having to call three different functions to only calling one (mariaView)
// I took //mariaItem,
//mariaTwo,
//mariaS,
//mariaG
// and wrapped them in one function mariaView that then uses async waterfall.
// This will NOT work with async parallel because parallel does all the functions at the same time, and the
// second, third and fourth functions need the info from the first function to succeed.
var mariaView = function (req, res, next){
@PistachioPony
PistachioPony / Using Async parallel
Created February 19, 2014 22:49
Change code to use Async Parallel
// Here mariaItem is called first and then moves onto mariaView.
// mariaView has two callbacks happening asynchronously but they need the info from mariaItem first.
var mariaItem = function (req, res, next) {
Item.findOne({ _id: "5099803df3f4948bd2f98391"}, function (err, result){
req.contents = {};
req.contents.result = result;
next();
});
};
@PistachioPony
PistachioPony / Node.js Terminals
Last active August 29, 2015 13:56
All the terminals I need to run Node.js
Terminal #1 run:
mongod --setParameter textSearchEnabled=true
Terminal #2 run:
npm start
Terminal #3 run:
@PistachioPony
PistachioPony / JS Prototyping
Last active August 29, 2015 13:56
How JS Prototype works
//See more here: http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work
//Define a functional object to hold persons in JavaScript
var Person = function(name) {
this.name = name;
};
//Add dynamically to the already defined object a new getter
Person.prototype.getName = function() {
return this.name;
@PistachioPony
PistachioPony / JS validation
Last active August 29, 2015 13:56
How the Validator Works
var mariaItem = function (req, res, next) {
Item.findOne({ _id: "5099803df3f4948bd2f98391"}, function (err, result){
req.contents = {};
req.contents.result = result;
next();
});
};
var mariaView = function (req, res, next){
async.waterfall([
@PistachioPony
PistachioPony / Modal to Route
Created March 11, 2014 15:17
Creating the modal html - routes
<!--In the Dashboard.njs-->
<div class="dashboard-item-offer box {{"invisible" if offer.isHidden}} {{"offer-to-rate" if offer.state == "rescinded" and not offer.ratedBySeller }}">
<div class="col col-1of4-md col-1of1-xs">
{% include "partials/userthumb.njs" %}
{% include "partials/modals/message.njs" %} *****This tells the button where to go
<div class="offerer">
<p class="offerer-name">{{offer.offerer.name}}</p>
<p class="offerer-location">{{offer.offerer.location.city}}, {{offer.offerer.location.state}}</p>
<a><button type="submit" class="bttn6 seller-message" data-toggle="modal" data-target="#modalSellerMessage"> Message</button></a>
@PistachioPony
PistachioPony / getting to know the form
Created March 19, 2014 19:26
gettting to know the form
{% extends '/layouts/layout.njs' %}
{% block contenttype%}item-form{%endblock%}
{% block content %}
{% if item.summary and item.title and (item.ship or item.exchange) and item.category and (item.images|length > 0) and (item.groups|length > 0) and (item.state != "open") %}
{% set isPublishable = 1 %}
{% else %}
{% set isPublishable = 0 %}
{% endif %}
@PistachioPony
PistachioPony / my group form.njs
Created March 19, 2014 19:29
my group form.njs
{% extends '/layouts/layout.njs' %}
{% block content %}
<div class="box ">
<aside class="container">
<div class="col col-1of1-xs">
<h1>Create a group</h1>
<br>
</div>
</aside>