0

I am getting the Error, I am writing a function to monitor a path for. I am new to Node.js:

TypeError: Cannot read property 'map' of undefined

at C:\Users\a\Desktop\DL\file\filemonitor.js:15:14 at FSReqWrap.oncomplete (fs.js:149:20)

const Promise = require ('bluebird');
var fs = Promise.promisifyAll(require("fs"));

monitordir(monitorpath) {
  var fileList = [];
  return new Promise((resolve, reject) => {
    fs.readdir(monitorpath,function(err, items) {
      items.map((file) => {
        fileList.push(file);
      });
      resolve(fileList);
    });
  })
}

Note: I don't see a package.json file either. Should I have a sucessful run to see it

1
  • So, what would ypu suggest i need to do to rectify it. I was told that Promise.promisifyAll you add a suffix Aysnc and hence i added a function to readdir Commented Apr 12, 2018 at 19:49

2 Answers 2

1

When you run var fs = Promise.promisifyAll(require("fs")); it return a promise to you. So you can't execute a map of a promise.

I believe that you don't need a Promise to resolve fs module, my suggestion is you write something like that.

const Promise = require('bluebird');
const fs = require("fs");

const monitordir = path => {
    return new Promise((resolve, reject) => {
        fs.readdir(path, (error, items) => {
            if (error) return reject(error)
            return resolve(items);
        })
    })
}
Sign up to request clarification or add additional context in comments.

2 Comments

So i change my code to this and i still get the same error const Promise = require('bluebird'); //var fs = Promise.promisifyAll(require("fs")); var fs = require('fs'); monitorDir(monitorPath) { var fileList = []; return new Promise((resolve, reject) => { fs.readdir(monitorPath, function(err, items) { items.map((file) =>{ fileList.push(file); }); resolve(fileList); }); }) }
Hey buddy! I created a gist to show you how I implemented the code above. gist.github.com/nicolastakashi/7cb5cbea674a586f644ee561e96d3ac6 I hope this helps
0

Try following fix, see if it fits your needs:

monitordir(monitorpath)
{
    var fileList = [];
    return fs.readdir(monitorpath)
        .then(function(err,items) {
            items.map((file) => {
                fileList.push(file); // fileList is ready here! do whatever you want before it resolves to caller
            });
            return fileList;
        })
        .catch(function(e) {
            // something bad happened; throw error or handle it as per your needs
            throw new Error(e);
        });
}

For package.json you can run npm init command at your project directory it will create one for you.

3 Comments

Found out the answer, the monitor path was not setup right
great, but still I would suggest you to try to drop additional return new Promise((resolve,reject) => { since you have promisified the fs module, it will look cleaner and better without extra code needed.
Thanks Zeeshan, will do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.