无法阅读财产' map'未定义的节点js

时间:2018-04-12 19:35:11

标签: javascript node.js fs

我收到错误,我正在编写一个监视路径的函数。我是Node.js的新手:

  

TypeError:无法读取属性' map'未定义的

     

在C:\ Users \ a \ Desktop \ DL \ file \ filemonitor.js:15:14    在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);
    });
    })
}

注意:我也没有看到package.json文件。我应该成功看到它吗

2 个答案:

答案 0 :(得分:0)

尝试以下修复,看看它是否符合您的需求:

   {
      "AWSTemplateFormatVersion":"2010-09-09",
      "Transform":"AWS::Serverless-2016-10-31",
      "Description":"my POC.",
      "Resources":{
        "MyApiStage":{
          "Type":"AWS::ApiGateway::Stage",
          "Properties":{
            "DeploymentId":{ "Ref":"ApiDeployment"},
            "MethodSettings":[
              {
                "DataTraceEnabled":true,
                "HttpMethod":"*",
                "LoggingLevel":"INFO",
                "ResourcePath":"/"
              }
            ],
            "RestApiId":{ "Ref":"MyApi" },
            "StageName":"Prod",
            "Variables":{
              "TAKE_ACTION" : {"Fn::Join" : 
                ["", 
                ["https://", 
                 {"Ref" : "ApiDeployment"}, 
                 ".execute-api.", 
                 {"Ref":"AWS::Region"}, 
                 ".amazonaws.com/", 
                 {"Ref":"MyApiStage"} 
              ] ]  }
            }
          }
        }
      }
    }

对于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); }); } ,您可以在项目目录中运行package.json命令,它将为您创建一个。

答案 1 :(得分:0)

当您运行var fs = Promise.promisifyAll(require("fs"));时,它会向您发送承诺。所以你不能执行承诺的地图。

我相信你不需要承诺来解决fs模块,我的建议是你写的那样。

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);
        })
    })
}