Month: June 2017

MONGODB: What is a projection?

Posted on Updated on

A projection is when you search for documents in MongoDB but only get back the fields that you request.

example records   {name: “Tom”, age: “35”, zipcode: “10003”,_id:778789754}

using projection for name and zipcode: {name: “Tom”,  zipcode: “10003”}

 

var mongo = require(“mongodb”).MongoClient;
var url = “mongodb://localhost/addressbook”;
var collectionName = “friends”;
mongo.connect(url,function(err,db){
if(err) console.log(err)//throw err;
else{
var collection = db.collection(collectionName);
collection.find({},{name: 1, age: 0,_id:0,zipcode: 0}).toArray( function(err,documents){
if(err) console.log(err)//throw err;
else{
console.log(documents)
}
});

db.close();
}
})

Advertisement

node notes: require() caches module.exports

Posted on Updated on

if you are importing a file that instantiates a new object

when you import the file again as a 2nd object you would expect … a new object

but your 2nd variable will be the same object as the first variable, even though a “new” constructor was used. why?

Node caches module imports , it wont load the file again so no new instance!!!

example:

// file greet3.js
function Greetr(){
  this.greeting=”Hello World from greet3″
  this.greet=function(){console.log(this.greeting)}
}
module.exports = new Greetr();

 

//app.js
var greet3 = require(“./greet3.js”);
greet3.greet();
greet3.greeting = “NEW GREETING!!!!”
var greet3a = require(“./greet3.js”);
greet3a.greet();
//console.log
Hello World from greet3
NEW GREETING!!!!
how to resolve??
in the file being exported, greet3.js, send the constructor not the new instance
//greet3.js
module.exports = Greetr;
//app.js
var Greet3a = require(“./greet3.js”);  //you capitalize greet3a to tell its constructor
var grtr = new Greet3a();

Node notes : on require with no js file in local path

Posted on

if you require, for example  “greet”, and greet.js is not found, node will look for folder called “greet” and then look for a file called index.js

 

 

Ubuntu: Install Visual Studio Code via apt-get

Posted on Updated on

Why fiddle with downloading from the browser when apt-get will do?

sudo add-apt-repository -y "deb https://packages.microsoft.com/repos/vscode stable main"
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EB3E94ADBE1229CF
sudo apt update
sudo apt -y install code

If need upgrade

sudo apt -y upgrade
sudo apt -y dist-upgrade

great answer from zurfyx

https://askubuntu.com/questions/616075/how-do-i-install-visual-studio-code

 

 

Errors encountered on my way to install Node.JS v8 on ubuntu

Posted on

when running apt-get install nodejs

You might want to run ‘apt-get -f install’ to correct these:
The following packages have unmet dependencies:
libgail-3-0 : Depends: libgtk-3-0 (= 3.20.9-1ubuntu2) but 3.18.9-1ubuntu4 is to be installed
nodejs : Depends: libuv1 (>= 1.6.1) but it is not going to be installed
python3-gi-cairo : Depends: python3-gi (= 3.22.0-1) but 3.20.0-0ubuntu1 is to be installed E: Unmet dependencies. Try ‘apt-get -f install’ with no packages (or specify a solution).

to fix:  apt-get -f install

When trying to install nodejs again will only allow for node-legacy (v4)

to fix: 

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

 

finally at v8 locally

 

io.emit vs socket.emit

Posted on

io.emit lets the server send an event to all the connected clients at once. This is useful when you need to send some data to everyone. This is something we want everyone to be able to see.

socket.emit sends an event to a single socket. This is great when you need to send an event to a single user. Example: This is why we use socket.emit when sending out a greeting message. It only needs to go to that one user.

 

socket.io notes

JavaScript Classes review

Posted on Updated on

Some basic notes

// *****START CLASS***********************************************

//The constructor function is called automatically when an instance of the class is created.
// a special method for creating and initializing an object created with a class.
// There can only be one special method with the name “constructor” in a class.
class Person {
constructor (name,age) { //this is a function
// console.log(name,age)
this.name=name;
this.age=age;
}
//methods
getUserDescription(){return `${this.name} is ${this.age} year(s) old`}
}

// *****END CLASS***********************************************

var me = new Person(“Andrew”,25); //arguments are passed to constructor function
// console.log(“this.name”, me.name)
// console.log(“this.age”, me.age)
var description = me.getUserDescription(); // Andrew is 25 year(s) old
console.log(description);

Location object

Posted on Updated on

Contains info about the URL

Part of the window object, also accessible by document object

Can access in dev tools by typing location

Items in location

  • search. : that is the query string 
  • pathname 
  • origin 
  • href 
  • port

See MDN for more
Examples: access seatch use window.location.search
See stevenbenner jQuery deparam for deconstruction of params  or go here 

Example deparam after loading function 

JQuery.deparam(window.location.search)

Continuous posting on blog

Posted on

Was watching a YouTube video by simple programmer where he pointed out that one should keep blogging and putting down their thoughts.  It shows that you have been working on a particular tech.

That’s was a good point. It reinforces your own learning and can help others who are also looking for information. When I was learning VMware I wrote tons of articles that were used by others. I would even surprise myself and find my own articles when looking for an answer.

Downloaded the WordPress app and will arrive for more as we all should