Web development

Uncaught TypeError: Cannot call method ‘replace’ of null (underscore.js:768)

Uncaught TypeError: Cannot call method ‘replace’ of null (underscore.js:768)

Cause

This error is throw when a Backbone View receive an invalid String as template, in template() function.

template: _.template($('.invalidSelector').html())

How to fix

  • Check the String is not null and not empty
  • If you are using jQuery, check the selector and the result of html()

More info Backbone View template() documentation

Standard
Web development

How to use BackboneJS templates in Jade

There is a problem when using BackboneJS templates in Jade, because of syntax. An example:

The problem

Example.html

div(style="display: none;")#example
    a(id='document_<%= id >') <%= title >

Example.js

var SampleView = Backbone.View.extend({
    template: _.template($('#example').html()), 
    render: function() { 
        var template = this.template({ 
            id: this.model.id, 
            title: this.model.get('title') 
        });
        $(this.el).html(template); 
        return this; 
    } 
});

This doesn’t work because Jade is compiling the content and the variables disappear.

The solution

There are a few tags that Jade doesn’t compile it: script and style.

When you use script, remember include the attribute type=’text/template’ else its code will be evaluate as Javascript and it will be generate error messages.

The example working:

script(type='text/template')#example
    a(id='document_<%= id >') <%= title >

More info Jade documentation

Standard
Web development

How to add parameters or variables to a BackboneJS view

It’s very easy to add parameters or variables to any BackboneJS view but being aware of what BackboneJS version are you using because it changed. Let’s see:

Before 1.1.0 version

Any parameter in the constructors went to the options variable of the view::

Controller

var userView = new UserView({
  mode: "add"
})

View

render: function() {
    // Use example in any function
    var mode = this.options.mode; // mode="add"
}

Now (version >= 1.1.0)

Just like above but adding this to the Controller:

initialize : function (options) {
  this.options = options || {};
}

You can check the BackboneJS changelog

Standard
Dev Ops

How to install or upgrade NodeJS to any version

expressnodejs

When you work in a few NodeJS projects, you may need an easy and fast way to install, switch or upgrade the NodeJS version. With NVM is possible.

Install

curl https://raw.github.com/creationix/nvm/master/install.sh | sh

Usage

Install a NodeJS version

nvm install 0.10.26

Switch or upgrade

nvm use 0.10.26

Use by default

nvm alias default 0.10.26

List the versions installed

nvm ls

By the way, if you get nvm: command not found check this solution

More info about NVM

Standard
Databases

Lost connection to Mysql server at ‘reading initial communication packet’, system error:0

mariadb-logo

Lost connection to Mysql server at ‘reading initial communication packet’, system error:0

This error appears when you try to connect to a MySQL or MariaDB database and it is unavailable. The fix depends on your situation:

If you have MySQL/MariaDB installed on the server where you are trying to connect

  • Edit the configuration file located on /etc/mysql/my.cnf
  • Set:
    bind_adress = 0.0.0.0
  • Restart:
    service mysql restart

If the server is just a proxy, like a HAProxy server:

  • Remember to grant access permissions on all database servers, check my guide about it on how to deploy a MariaDb Cluster:
    INSERT INTO USER (Host,USER) VALUES ('11.11.11.11','haproxy');
    FLUSH PRIVILEGES;

    Repeat it for each db server, changing IP and proxy user.

Standard