Node.js 从数据库获取文件后,无法将项推入数组

Node.js 从数据库获取文件后,无法将项推入数组,node.js,filesystems,Node.js,Filesystems,我从一个名为file_uploads from Database的集合中获取文件。同时,我从另一个位置获取目录文件,并将其存储在files_to_read[]中。循环通过两个文件,我匹配文件,然后将其推送到文件\u到\u模拟数组 const fs = require('fs') let folder = '../../importedFiles/data'; let files_to_read = [] let files = fs.readdirSync(folder); let fileSc

我从一个名为file_uploads from Database的集合中获取文件。同时,我从另一个位置获取目录文件,并将其存储在files_to_read[]中。循环通过两个文件,我匹配文件,然后将其推送到文件\u到\u模拟数组

const fs = require('fs')
let folder = '../../importedFiles/data';
let files_to_read = []
let files = fs.readdirSync(folder);
let fileSchema = require('../db/models/fileUpload');
let files_to_simulate = [];

files.forEach(file => {
    let fileStat = fs.statSync(folder + '/' + file).isDirectory();
    if(!fileStat) {
        files_to_read.push(file);
    }
});

fileSchema.find({}, async function(err, files) {
    files.forEach(function(file) {
        files_to_read.forEach(function(directoryFile) {
            var dbFile = file.name.split('.');
            let dirFile = directoryFile.split('.');
            if (dbFile[0] === dirFile[0]) {
                console.log('saa')
                files_to_simulate.push(dirFile[0]);
            }
        })
    })
});

console.log(files_to_simulate)

更改代码的顺序,它将正常工作:

const fs = require('fs')
let folder = '../../importedFiles/data';
let files_to_read = []
let files = fs.readdirSync(folder);
let fileSchema = require('../db/models/fileUpload');
let files_to_simulate = [];

files.forEach(file => {
    let fileStat = fs.statSync(folder + '/' + file).isDirectory();
    if(!fileStat) {
        files_to_read.push(file);
    }
});

fileSchema.find({}, async function(err, files) {
    files.forEach(function(file) {
        files_to_read.forEach(function(directoryFile) {
            var dbFile = file.name.split('.');
            let dirFile = directoryFile.split('.');
            if (dbFile[0] === dirFile[0]) {
                console.log('saa')
                files_to_simulate.push(dirFile[0]);
            }
        })
    })
// If you want to have the result you need to call a function here
console.log(files_to_simulate)
});
如果要导出,您需要:

const fs = require('fs')
let folder = '../../importedFiles/data';
let files = fs.readdirSync(folder);
let fileSchema = require('../db/models/fileUpload');
// in the destination file just give it a callback
module.exports = function(callback){
let files_to_read = []
let files_to_simulate = [];

files.forEach(file => {
    let fileStat = fs.statSync(folder + '/' + file).isDirectory();
    if(!fileStat) {
        files_to_read.push(file);
    }
});

fileSchema.find({}, async function(err, files) {
    files.forEach(function(file) {
        files_to_read.forEach(function(directoryFile) {
            var dbFile = file.name.split('.');
            let dirFile = directoryFile.split('.');
            if (dbFile[0] === dirFile[0]) {
                console.log('saa')
                files_to_simulate.push(dirFile[0]);
            }
        })
    })
// If you want to have the result you need to call a function here
callback(files_to_simulate)
});
}

在这里,您可以阅读有关更改代码顺序的信息,它将正常工作:

const fs = require('fs')
let folder = '../../importedFiles/data';
let files_to_read = []
let files = fs.readdirSync(folder);
let fileSchema = require('../db/models/fileUpload');
let files_to_simulate = [];

files.forEach(file => {
    let fileStat = fs.statSync(folder + '/' + file).isDirectory();
    if(!fileStat) {
        files_to_read.push(file);
    }
});

fileSchema.find({}, async function(err, files) {
    files.forEach(function(file) {
        files_to_read.forEach(function(directoryFile) {
            var dbFile = file.name.split('.');
            let dirFile = directoryFile.split('.');
            if (dbFile[0] === dirFile[0]) {
                console.log('saa')
                files_to_simulate.push(dirFile[0]);
            }
        })
    })
// If you want to have the result you need to call a function here
console.log(files_to_simulate)
});
如果要导出,您需要:

const fs = require('fs')
let folder = '../../importedFiles/data';
let files = fs.readdirSync(folder);
let fileSchema = require('../db/models/fileUpload');
// in the destination file just give it a callback
module.exports = function(callback){
let files_to_read = []
let files_to_simulate = [];

files.forEach(file => {
    let fileStat = fs.statSync(folder + '/' + file).isDirectory();
    if(!fileStat) {
        files_to_read.push(file);
    }
});

fileSchema.find({}, async function(err, files) {
    files.forEach(function(file) {
        files_to_read.forEach(function(directoryFile) {
            var dbFile = file.name.split('.');
            let dirFile = directoryFile.split('.');
            if (dbFile[0] === dirFile[0]) {
                console.log('saa')
                files_to_simulate.push(dirFile[0]);
            }
        })
    })
// If you want to have the result you need to call a function here
callback(files_to_simulate)
});
}


在这里,您可以阅读有关

的内容,它工作正常,但console.log不会记录响应,因为fileSchema的回调是一个异步函数,这就是我希望dirfile[0]存储在files\u to\u模拟数组中的问题。所以我使用console.log(files-to-simulate)查看它是否存储在数组中。但不幸的是,它没有被存储。你能帮我解决这个问题吗?正如我说的,console.log是同步执行的,但是你的回调函数是异步执行的。因此,您无法看到结果,但代码工作正常,文件以异步方式用结果填充数组。但是我的时间不多了。这段代码是否可以更具体地以异步方式工作?我刚刚添加了答案,并让我知道它是否工作正常。但是console.log没有记录响应,因为fileSchema的回调是一个异步函数。这就是我希望dirfile[0]存储在files\u to\u模拟数组中的问题。所以我使用console.log(files-to-simulate)查看它是否存储在数组中。但不幸的是,它没有被存储。你能帮我解决这个问题吗?正如我说的,console.log是同步执行的,但是你的回调函数是异步执行的。因此,您无法看到结果,但代码工作正常,文件以异步方式用结果填充数组。但是我的时间不多了。是否可以更具体地让这段代码以异步方式工作?我刚刚添加了答案,并让我知道它是否工作。是的,此控制台记录所需的输出。但是要将结果存储在文件中,我必须做什么?。因为我将把文件_to _simulate数组导出到另一个文件中,以用于模拟目的示例module.exports={files:files _to _simulate}这是另一个问题。您需要将代码包装到函数中,然后返回结果并导出该函数。我的主要目的是将结果存储在数组中,而不是控制台记录输出。!你能帮忙吗?你需要把你的代码包装在一个函数中,然后导出这个函数。我这样做了,但是它返回了promiseYes,这个控制台记录了所需的输出。但是要将结果存储在文件中,我必须做什么?。因为我将把文件_to _simulate数组导出到另一个文件中,以用于模拟目的示例module.exports={files:files _to _simulate}这是另一个问题。您需要将代码包装到函数中,然后返回结果并导出该函数。我的主要目的是将结果存储在数组中,而不是控制台记录输出。!你能帮忙吗?你需要把你的代码包装成一个函数,然后导出这个函数