Plates 0.4.8 Issue with CharAt #90

Closed
iwarner opened this Issue Nov 1, 2012 · 7 comments

Projects

None yet

4 participants

@iwarner

Hi

Receiving this

/vagrant/node_modules/plates/lib/plates.js:248
c = html.charAt(i);

with this code

    var match = { "taskList" : "New Value" };
    var output = Plates.bind(data, match);

data being a full HTML template

Appreciate the guidance

@3rd-Eden
a decoupled application framework member

What is the actual issue? You didn't post the error message or even a stack trace.

@LeytonC

I think i have had the same problem, i opened a html file using File System object, and when the readFile method called back with the data, the returned data is not explicitly a string object and of course chatAt method is not present. I fixed it by using the string() function when i made the call .bind to plates, this fixed the 'problem' (its not really a problem, it just needs the right type passed.)

Here is the callstack from node when you pass without a type cast to string.


has no method 'charAt'
at Object.bind (/Users/leytonc/rss/node_modules/plates/lib/plates.js:255:18)
at Object.bind (/Users/leytonc/rss/node_modules/plates/lib/plates.js:666:18)

i cut my functions names here since they will be pointless.

at fs.readFile (fs.js:LineNo:Place)
at Object.oncomplete (fs.js:297:15)

(Sorry if this is obvious, but i am not that experienced with node, an it looks like someone else had the same problem, hopefully it might help someone else.)

@Southern

@LeytonC We need to see what you're trying to do in order to know what's going on. So far we just know that .bind isn't working because .charAt is throwing an error.

The only thing I can assume is that you're getting a buffer returned.

For example:

var html = '<span>This is valid code.</span>';

// This is what is expected.
var template = plates.bind(html);

// This is what's happening. It'll throw an error.
var template = plates.bind(new Buffer(html));

// This is what should be done to avoid the error.
var template = plates.bind((new Buffer(html)).toString());
@LeytonC

That is the exact problem, when i read the documentation for file system, i did not realise that the data returned was a buffer. Please consider the example code below

// Example of problems with .chatAt
var plates = require('plates');
var fs = require('fs');

// Read in the file
fs.readFile('./example.html', function(err, data) {

    // What data type do we have?
    console.log('We have a type of: '+data.constructor.name);

    // and now we should have the problem
    plates.bind(data);  

});

the data is a buffer not a string, so you get the error.

@3rd-Eden
a decoupled application framework member

You should specify a encoding when you fs.readFile for example fs.readFileSync('./example.html', 'utf-8') Not sure if this issue is worth fixing in Plates.

@LeytonC

I added the encoding, and now i am getting a string back. Since this is technically not a plates issue, its a user issue there is nothing to fix, but its probably worth mentioning it somewhere in documentation or examples would be a good / better idea.

@Southern

@LeytonC The fs module returning buffers is documented in Node. http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback

@3rd-Eden 3rd-Eden closed this in #99 May 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment