1

I'm trying to build a node cli to keep an organized planner. I have 'locate' command that goes to the options for commands either 'find' or 'list', but no data is being shown even though the mongoose functions are working correctly. 'todo locate list 12/31/1999' does not return any data back even though there are dates greater than that in my database. Coud someone help me debug this project. The GitHub repository is github.com/tripiod8/todo_cli

const mongoose = require('mongoose');
const Todo = require('../model/todoSchema');

// Find todo
const findTodoIn = (_tag) => {
   Todo.find({ tag: "Birthday" })
      .then(todo => {
         console.info(todo);
         console.info(`${todo.length} matches`);
         mongoose.disconnect();
      }).catch(err => console.error(err));
};

  // List todo
const listTodoIn = (_date) => {
   Todo.find( { date: { $gte: _date } } )
      .then(todo => {
         console.info(todo);
         console.info(`${todo.length} matches`);
         mongoose.disconnect();
      }).catch(err => console.error(err));
};

module.exports = {
   findTodoIn,
   listTodoIn
};
const program = require('commander');
const {findTodoIn, listTodoIn} = require('../controllers/locate');

program
.command('find <tag>')
//.alias('f')
.description('Find a todo')
.action(_tag => findTodoIn(_tag))

 program
 .command('list <_date>')
// .alias('l')
.description('List a todo')
.action(_date => listTodoIn(_date));

program.parse(process.argv);
const program = require('commander');

program
.version(pkg.version)
.command('locate', 'Locate a todo')

 program
  .command('add')
  .alias('a')
  .description('Add a todo')
  .action(() => {
      prompt(questions).then((answers) => {
          addTodoIn(answers)
      })
  });


program.parse(process.argv);
Tripiod8
  • 99
  • 6
  • You may want to update to using `async` functions, avoiding the `then` mess. Also an argument prefix of `_` means "not used" in most contexts, though you're clearly using them here. – tadman May 22 '20 at 01:42
  • Have you tried `Todo.find( { date: { $gte: new Date(_date) } } )`? – thammada.ts May 22 '20 at 03:37
  • I have been running some debugging and my decision is it has nothing to do with the arguments. I have even been trying just a find() and that is not showing data either. It has something to do with async/await I believe. let findTodoIn = async () => { let updateResult = await Todo.find() console.log(`${updateResult} or hello`); }; Even trying this does not output any data. – Tripiod8 May 22 '20 at 03:40
  • Are you connecting to the right database? Does `findTodoIn` work? – thammada.ts May 22 '20 at 05:06
  • It does work. I can add and remove from the database. I just added a second directory to the cli hierarchy that is not working. – Tripiod8 May 22 '20 at 06:40
  • 1) Is it related to going through Commander? If you call the code directly without going through CLI and Commander does it work? – shadowspawn May 22 '20 at 22:22
  • 2) Commander allows async action handlers in which case you should call .parseAsync rather than .parse (and handle the returned promise). – shadowspawn May 22 '20 at 22:24
  • Yea I can query what I'm looking for in Mongo and if I put the 'find' commands in the file with the other commands it works fine. Once I move them to there own file then the commands don't work. I took your suggestion with parseAsync but I have not got it to work for me. – Tripiod8 May 23 '20 at 02:05
  • You may have a problem with dangling promises not keeping the application running. If you add a "sleep" using setTimeout to your main application, do you then see results? – shadowspawn Jun 01 '20 at 08:32
  • I used promisify to try the setTimeout and it returned undefined, which is different than it usually not returning anything. I have this on GitHub if you would be interested in taking a look? It's pretty upsetting that I cannot fix this issue. github.com/tripiod8/todo_cli – Tripiod8 Jun 01 '20 at 18:07
  • 1
    I think I see a cause. You are not calling `mongoose.connect` anywhere in your sample code, and looking at your repo, it also does not get called when the `locate` subcommand is run. Your `todo` command connects to the database through a side-effect when `index.js` is loaded. However, the `locate` subcommand is located in a stand-alone executable and does not load that file or connect to the database. – shadowspawn Jun 05 '20 at 23:35
  • You sir were right and found the problem! I added the MongoDB connection to my locate.js and it returned my data successfully! Thank you for helping me and now I should have a better chance of having my todos arranged! – Tripiod8 Jun 06 '20 at 04:17

0 Answers0