我收到错误,我正在编写一个监视路径的函数。我是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
文件。我应该成功看到它吗
答案 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);
})
})
}