【发布时间】:2023-03-07 13:01:01
【问题描述】:
我正在使用 NodeJs 和 Box SDK。
我需要获取目录中所有文件和文件夹的数据,因此我正在检查每个文件夹中的子文件夹和文件,将数据保存在数组中,如果有任何子文件夹,则再次调用该函数.
问题是我无法从递归调用中获取包含所有数据的数组,我无法确定在哪里返回或解析我的函数checkChilds,也无法处理承诺。
我需要的是一个承诺,它会在 for 循环完成并且所有堆栈完成后解决,因此 arrayIds 将在一切完成后拥有所有数据。
我的主目录中的文件夹结构分为预览和生产,所以我也在检查它在数组中的位置。
这是我的代码:
var boxUtil = require('../utils/boxUtils')
var boxInstance = boxUtil.getBOXInstance()
exports.script = function (request, response) {
var accessToken = request.headers.authorization
var folderId = request.body.folderId
var userId = request.body.user
var arrayIds = []
boxInstance = boxUtil.getBOXInstance()
if (boxInstance == null) {
boxUtil.initBOXInstance(userId, function (error, res) {
if (error) {
response.status(500)
return response.send(error.message)
}
boxInstance = res
var box = boxInstance.getBasicClient(accessToken)
var func = getFolderInfo(folderId, arrayIds, box)
.then(checkIfPreviewOrProduction)
.then(insertInPreviewOrProduction)
.then(iAmRecursive)
.then(stringLogMe)
.then(null, console.log)
})
} else {
console.log('RESTART SERVER AND TRY AGAIN PLEASE')
}
}
function iAmRecursive ([resp, arrayIds, boxInstance]) {
return new Promise(function (resolve, reject) {
function checkChilds ([resp, arrayIds, boxInstance]) {
if (resp.item_collection.total_count > 0) {
for (var j = 0; j < resp.item_collection.entries.length; j++) {
if (resp.item_collection.entries[j].type === 'folder') {
getFolderInfo(resp.item_collection.entries[j].id, arrayIds, boxInstance)
.then(insert)
.then(checkChilds)
.then(null, console.log)
}
if (resp.item_collection.entries[j].type === 'file') {
ifFileChild([resp.item_collection.entries[j], arrayIds, boxInstance])
}
}
} else {
console.log('no childs were found')
}
}
checkChilds([resp, arrayIds, boxInstance])
})
}
function stringLogMe (stringifyMe) {
var text = console.log(JSON.stringify(stringifyMe, null, 2))
return text
}
function getFolderInfo (folderId, arrayIds, boxInstance) {
return new Promise(function (resolve, reject) {
var query = {fields: 'id,name,item_collection,path_collection'}
boxInstance.folders.get(folderId, query, function (err, resp) {
if (err) {
reject(err)
} else {
resolve([resp, arrayIds, boxInstance])
}
})
})
}
function getMetadata ([resp, arrayIds, boxInstance]) {
return new Promise(function (resolve, reject) {
var fileId = resp.id
boxInstance.files.getAllMetadata(fileId, function (err, response) {
if (err) {
reject(err)
} else {
response.name = resp.name
response.type = resp.type
resolve([response, arrayIds, boxInstance])
}
})
})
}
function insert ([resp, arrayIds, boxInstance]) {
return new Promise(function (resolve, reject) {
if (resp.type === 'file') {
var newObject = {
'id': resp.id,
'name': resp.name,
'type': resp.type,
'entries': resp.entries
}
} else {
var newObject = {
id: resp.id,
name: resp.name,
type: resp.type
}
}
arrayIds.push(newObject)
resolve([resp, arrayIds, boxInstance])
})
}
function ifFileChild ([resp, arrayIds, boxInstance]) {
return new Promise(function (resolve, reject) {
getMetadata([resp, arrayIds, boxInstance])
.then(insert)
.then(null, console.log)
})
}
我的函数iAmRecursive 应该是用承诺链中的完整数组来解决的。
我已经阅读了几个类似的问题,但我无法使用递归调用进行任何操作。
如果您需要对问题进行更多说明,请告诉我。
【问题讨论】:
-
这是很多代码。您可以尝试隔离问题吗?甚至没有看它或了解问题所在:首先递归收集所有名称,然后在结果上使用
Promise.all。 -
感谢您的宝贵时间!我只是编辑了代码以使其更简单。问题是,我怎么知道递归函数什么时候会收集到所有的名字?因为现在我的函数
iAmRecursive和checkChilds没有返回或解析任何结果。我已经尝试过多次解决承诺,但没有奏效。我应该返回checkChilds吗?同时解决?
标签: javascript node.js recursion