【发布时间】:2020-02-07 04:19:19
【问题描述】:
我是 Node 和 Firebase 的新手。
我目前正在开发一款游戏的制作计算器,并将游戏物品存储在 Firebase 中。有些项目是复合项目,例如:
1 Lumber Plank = 5 Logs
根据这些要求,我将所有项目构建为一个集合,在 Firebase 中命名为 items。
日志将被持久化为:
{
"type": "basic",
"name": "log"
}
而木板是:
{
"type": "composite",
"name": "lumber plank",
"materials": ["log"],
"material_values": [5]
}
有了这样的结构,我试图通过递归搜索数据库来构建一个制作树。最终结构如下所示:
{
"name": "board",
"count": 1,
"materials": [
{
"name": "lumber plank",
"count": 1,
"materials": [
{
"name": "log",
"count": 5,
"materials": null
}
]
}
]
}
我在调试时无法理解回调,这段代码目前返回undefined,后跟log(我假设这来自search函数中的console.log)。
async function search(item, result, count) {
let calcItem = {
name: item,
count: count
};
db.collection("items")
.doc(item)
.get()
.then(doc => {
const data = doc.data();
if (data.type === basic) {
calcItem.materials = null;
result.push(calcItem);
return result;
} else {
let materials = data.materials;
let materialsCount = data.material_values;
calcItem.materials = [];
for (let i = 0; i < materials.length; i++) {
console.log(materials[i]);
search(materials[i], calcItem.materials, materialsCount[i]);
}
}
});
}
let item = "lumber plank";
search(item, [], 1).then(result => console.log(result));
不胜感激这里的任何指针/提示。谢谢
根据 Doug 的反馈,
我已经根据你的 cmets 重构了我的代码,并且我看到了一些进展。
function recursiveSearch(item, count, result) {
let calcItem = {
name: item,
count: count
};
dbSearch(item).then(function (doc) {
const data = doc.data();
console.log(data);
if (data.type === basic) {
calcItem.materials = null;
result.push(calcItem);
return result;
} else {
let materials = data.materials;
let materialsCount = data.material_values;
calcItem.materials = [];
for (let i = 0; i < materials.length; i++) {
recursiveSearch(materials[i], materialsCount[i], calcItem.materials);
}
}
});
}
function dbSearch(item) {
return Promise.resolve(db.collection("items")
.doc(item)
.get()
.then());
}
Log 现在正确输出搜索。
{
material_values: [ 5 ],
materials: [ 'log' ],
name: 'lumber plank',
type: 'composite'
}
{
name: 'log',
type: 'basic'
}
但是,如果我理解正确,如果我在这一行中添加它仍然会返回 undefined,对吗?
console.log(recursiveSearch("lumber plank", 1, [])
如果是这样,我如何在完成所有递归搜索的同时实际注销整个项目结构?
对不起,如果这个问题听起来有点愚蠢。我主要来自 Java 背景,处理 promises/async/await 对我来说是全新的
【问题讨论】:
标签: javascript node.js firebase google-cloud-firestore