Friday, March 28, 2014

Would understanding streams help avoid errors with Readline?

When I try to run this: var readline = require('readline'); var rl = readline.createInterface({ input: process.sdtin, output: process.stdout, }); rl.question("What do you think of node.js?", function(answer) { // TODO: Log the answer in a database console.log(answer + ',Thank you for your valuable feedback.'); // These two lines together allow the program to terminate. Without // them, it would run forever. i.close(); process.stdin.destroy(); }); I get this: readline.js:833 if (stream._keypressDecoder) return; ^ TypeError: Cannot read property '_keypressDecoder' of undefined at Object.emitKeypressEvents (readline.js:833:13) at new Interface (readline.js:118:13) at Object.exports.createInterface (readline.js:39:10) at Object. (/home/shambaughlab/node_examples/seh1_readline.js:3:19) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) I am unsure, but maybe I should look at streams. http://nodejs.org/api/stream.html

Sunday, March 16, 2014

WebPayments Use Cases

I've been spending the past few weeks with others such as Natasha Rooney, Marcos Caceres, and Manu Sporny working on some WebPayments Use Cases for the first W3C Webpayments workshop (1). They are illustrated here: https://www.w3.org/community/webpayments/wiki/WebPaymentsMobileUseCases , but also here: https://github.com/bshambaugh/payments-use-cases and here: https://github.com/w3c-webmob/payments-use-cases . I'm trying to get things narrowed down so that Marcos' suggestions can be included: http://lists.w3.org/Archives/Public/public-webpayments/2014Mar/0082.html

(1) http://www.w3.org/2013/10/payments/

Saturday, March 1, 2014

Control Flow in Node.js -- this is not working

When I run this code, I only see the first readline with, "Should I make a career change?". Perhaps this has something to do with program flow in the language.

Here is the code:

readline = require('readline');

var fs = require('fs');

var answer = 'the wrong answer';

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question("Should I change?", function(answer) {
write(answer + ', I should change.');
  rl.close();
});

 rl.question("Should I stay the same?", function(myanswer) {
 mywrite(myanswer + ', I should stay the same.');
  rl.close();
});

function write(answer) {
fs.writeFile('career_change.txt', answer , function(err) {
});
}

function mywrite(myanswer) {
fs.writeFile('career_change.txt', myanswer , function(err) {
});
}

Here is Mixu's chapter on control flow in Node.js:

http://book.mixu.net/node/ch7.html