node.js - if statement get skipped in javascript -
i writing basic application value arguments , display them. have 2 files:
app.js
console.log('application launching'); const fs = require('fs'); const yarg = require('yargs'); const node = require('./node.js') var command = yarg.argv; if (command === '3' ) { console.log("adding note"); node.addnote(argv.name,argv.title); } else { console.log('invalid'); }
node.js
console.log("im up") var addnote = (name,title) => { console.log('welcome', title, name); }; module.export = { addnote }
this output when pass argument:
admins-mac:node admin$ node app.js --3 tony mr
application launching
im up
invalid
if knowledge right, output must welcome mr tony
.
i can't figure out error.
yargs giving object of parameters. need check
if (command[3]) { // ... }
however, we'd have errors here
node.addnote(argv.name,argv.title);
as did't pass anything, , neither argv.name defined nor argv.title.
so given command:
node app.js --3 --name=tony --title=mr
you need code:
let command = yarg.argv; if (command[3]) { console.log("adding note"); addnote(command.name,command.title); }
third, don't require nodejs. it's environment. instead, need require file holding second code block.
const addnote = require("./2nd.js");
assumed file called 2nd.js , in same folder.
to wrap things (there number of other errors in code) , here's working rewrite of code:
1st.js:
const fs = require('fs'); const yarg = require('yargs'); const addnote = require("./2nd.js"); let command = yarg.argv; if (command[3]) { console.log("adding note"); addnote(command.name, command.title); } else { console.log('invalid'); }
2nd.js:
let addnote = (name,title)=>{ console.log(`welcome, ${title} ${name}`); }; module.exports = addnote;
run with
node 1st.js --3 --name=tony --title=mr
Comments
Post a Comment