Some handy functions in mongoose
-
Pagination with mongoose
npm install mongoose-paginate Model.paginate({}, { page: 3, limit: 10 }, function(err, result) { //result.docs // result.total // result.limit - 10 // result.page - 3 // result.pages });
Some handy functions in mongoose
npm install mongoose-paginate Model.paginate({}, { page: 3, limit: 10 }, function(err, result) { //result.docs // result.total // result.limit - 10 // result.page - 3 // result.pages });
Components are the core of Angular2. It is important and interesting to understand how components interact. I will try to explain the components’ interaction here.
We have 2 components here.
The child component is contained inside the parent component. It looks like the one below
Let’s consider the cases:
Case 1: The child to parent communication. On click on button “Send message to Mom” button, the message “Hi Mom! Greetings from daughter” is passed from the child to the parent component. It looks like
To achieve this, we do the following at the child component
@Output() callMomEvent = new EventEmitter();
So, putting it together, the child component looks like as follows
import { Component, EventEmitter,Output } from '@angular/core'; @Component({ selector: "daughter-component", template: `<div style="border:blue 2px solid; width:300px;margin-left: 51px;margin-bottom: 24px;"> <h3> I am Child comoponent, the Daughter</h3> <div *ngIf="messageFromMom">Message from Mom: {{messageFromMom}}</div> <div><button (click)="callMom()">Send message to Mom</button></div> </div>` }) export class ChildComponent { // Create EventEmitter object @Output() callMomEvent = new EventEmitter(); // emit the event callMom() { this.callMomEvent.emit("Hi Mom! Greetings from daughter"); } }
And at the parent component, we do the following
<daughter-component (callMomEvent)="handleCallMom($event)"></daughter-component>
The callMomEvent is the event fired by the child component and handleCallMom is the function that handles the event. It is defined in the parent component
Putting it together, the parent component looks like as follows
import { Component } from '@angular/core'; @Component({ selector: 'parent-component', template: `<div style="border:red 2px solid; width:800px;"> <h3>I am parent component, the Mom</h3> <daughter-component (callMomEvent)="handleCallMom($event)"></daughter-component> <div *ngIf="messageFromChild"> <h3>Message from Child: </h3> {{messageFromChild}} </div> </div>`, styleUrls: ['./app.component.css'] }) export class ParentComponent { private messageFromChild; handleCallMom(message) { this.messageFromChild = message; } }
That’s it
Case 2: Passing data from the parent component to child component
That’s simple. It’s done with input binding. Steps are as follows
@Input() messageFromMom : string;
<daughter-component [messageFromMom]="greetings" (callMomEvent)="handleCallMom($event)"></daughter-component></div>
That’s it
import { Component, ElementRef, AfterViewInit } from '@angular/core'; declare const gapi: any; @Component({ selector: 'google-signin', template: 'Google Sign-In' }) export class GoogleSigninComponent implements AfterViewInit { private clientId: string = ''; private scope = [ 'profile', 'email', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/contacts.readonly', 'https://www.googleapis.com/auth/admin.directory.user.readonly' ].join(' '); public auth2: any; public googleInit() { let that = this; gapi.load('auth2', function () { that.auth2 = gapi.auth2.init({ client_id: that.clientId, cookiepolicy: 'single_host_origin', scope: that.scope }); that.attachSignin(that.element.nativeElement.firstChild); }); } public attachSignin(element: any) { let that = this; that.auth2.attachClickHandler(element, {}, function (googleUser: any) { that.authService.loginWithGooglePlus(googleUser.getAuthResponse().id_token) .then((response: any) => { debugger; //that.authService.saveloggedInUser(response.user); //that.authService.setUserToken(response.token); }); }, function (error: any) { console.log(JSON.stringify(error, undefined, 2)); }); } constructor(private element: ElementRef, private authService: AuthService ) { console.log('ElementRef: ', this.element); } ngAfterViewInit() { this.googleInit(); } }
Node.js has a short release cycle which is good but sometimes it breaks things and deprecates a lot of stuffs.So, as a developer we would like maintain different versions of node.js. nvm is a handy tool to do that. We can install any version of node.js and also switch to a version accordingly. Let’s try nvm and switch versions
To install nvm
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
To load nvm
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
source ~/.bashrc
– Now to install a node.js version say 6.3.1.
nvm install v6.3.1
– To switch to a version
nvm use v6.3.1
– To see the list of installed versions
nvm list
The currently used version is v6.3.1 marked by ->
-> v6.3.1 v6.9.5 v7.4.0 system node -> stable (-> v7.4.0) (default) stable -> 7.4 (-> v7.4.0) (default) unstable -> 6.9 (-> v6.9.5) (default) iojs -> iojs- (-> N/A) (default)
– To remove a version
nvm uninstall v6.3.1
Inspired by Clean Code
Today’s error is the following:
localhost:8100/#/app/login:1 XMLHttpRequest cannot load http://mysite.local/api/login. Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8100’ is therefore not allowed access.
I was working on ionic & everything was working fine. Then I tried to use a login API. Suddenly from nowhere, it threw up the error . I guessed, it’s from the server side. But, again it works fine from the apk . Hmm, strange!
Then I installed a chrome-app Allow-control-allow-origin . And then, they he coded happily thereafter. Happy ending for the day!
Today I was trying to debugging on Device from ubuntu 14.04 & I was badly stuck.
Being new to Android, I was in the setup & debugging maze.
Now I was trying to debug from my smart phone.
sudo apt-get install android-tools-adb
sudo apt-get install android-tools-adb
SUBSYSTEM=="usb", ATTRS{idVendor}=="0bb4", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0e79", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0502", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0b05", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="413c", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0489", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="091e", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0bb4", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="12d1", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="24e3", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="2116", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0482", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="17ef", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="1004", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="22b8", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0409", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="2080", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0955", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="2257", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="10a9", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="1d4d", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0471", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="04da", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="05c6", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="1f53", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="04e8", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="04dd", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0fce", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="0930", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="19d2", MODE="0666" SUBSYSTEM=="usb", ATTRS{idVendor}=="1bbb", MODE="0666"
sudo cp /tmp/android.rules /etc/udev/rules.d/51-android.rules sudo chmod 644 /etc/udev/rules.d/51-android.rules sudo chown root. /etc/udev/rules.d/51-android.rules sudo service udev restart sudo killall adb
adb devices
to confirm that now it has permission to access the phone.
Then I had to downgrade to PHP 5.6
There are 2 ways to do that. Remove PHP 7.0 completely & install PHP 5.6
sudo apt-get remove php7.0* sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install php 5.6
Keep both PHP 7.0 & PHP 5.6, switch accordingly
sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring libapache2-mod-php5.6 libapache2-mod-php7.0
Now to use PHP7.0 use the following command
sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart
And to use PHP5.6 use the following
sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart
That’s it!
To do: Setting up Doctrine to use Oracle
So, here we go.
config/autoload/local.php
& add the configuration as follows
return array( 'doctrine' => array( 'connection' => array( 'orm_default' => array( 'driverClass' => 'Doctrine\DBAL\Driver\OCI8\Driver', 'params' => array( 'host' => '192.168.4.136', 'port' => '1521', 'user' => 'iamuser', 'password' => 'xxxx', 'dbname' => 'mydb', 'driver' => 'oci8', 'service' => true, 'servicename'=>'godisgreat' ) ) ) ) );
sudo apt-get install libaio1
Then install the oci8 driver . You can dowload it from Oracle’s site
sudo vi /etc/php5/apache2/php.ini
& add the extension like
extension=oci8.so
Doctrine ORM expects every table to be an Entity. But that is not easy , when dealing with complex query and which requires querying the result..
So, a hack to this problem is Resultsetmapping class, which allows you to write sql query and then map the result to Doctrine… Here is an example:
use Doctrine\ORM\Query\ResultSetMapping; $query = "select q1.id as id,q1.display_id as display_id,q1.product_id from (SELECT * FROM ( SELECT id, display_id,active_flag,hash,product_id FROM test_case where product_id=$productId and active_flag=0 ORDER BY display_id DESC ) a GROUP BY hash ) AS q1 join (SELECT * FROM (SELECT id,display_id,active_flag,hash FROM test_case where product_id=$productId ORDER BY display_id DESC) b GROUP BY hash ) AS q2 on q1.id = q2.id"; $rsm = new ResultSetMapping(); $rsm->addEntityResult('Test\Entity\TestCase', 'TestCase'); $rsm->addFieldResult('TestCase', 'id', 'id'); $rsm->addFieldResult('TestCase', 'display_id', 'displayId'); $rsm->addFieldResult('TestCase', 'product_id', 'productId'); $query = $this->getEntityManager()->createNativeQuery($query, $rsm); $testCaseDrafts = $query->getResult();