我如何将列表与嵌套的 foreach 一起使用?
How can i use list with nested foreach?
var app = express();
var cloud = firebase.firestore();
app.get('/getMyDatas', function (req, res) {
let tList = [];
let tJson = {};
cloud.collection('contents/').orderBy('date').get().then((contents) => {
contents.docs.forEach(cont=> {
cloud.collection('userprofile/').where('userId', '==', cont.data().userId).get().then((users) => {
users.docs.forEach(user => {
tJson = {description:cont.data().description, name:user.data().name};
tList.push(tJson);
tJson = {};
console.log("LIST IS FILLED SUCCESFULLY : " + JSON.stringify(tList));
});
});
});
console.log(" ??HERE THE LIST IS EMPTY : " + JSON.stringify(tList));
res.json(tList);
});
});
这段代码可以创建我想要的列表。但是我不能在线上使用它
上面写着“res.json(tList)”。
我可以在“console.log('My List : ' +
JSON.stringify(tList));"(它正确显示了我的列表。)
res.json(tList) return "[]" 空列表。我怎么能用这个
线?
This code can create the list i want. But i can't use it on the line that says "res.json(tList)".
在尝试使用您在异步操作中构建的结果之前,您没有正确等待异步操作完成。因此,您尝试在添加任何项目之前使用 tList
(时间问题)。
对于任何可能被拒绝的承诺,您也没有任何适当的错误处理。
如果您切换到 for
循环,然后您可以使用 async/await
对您的异步操作进行排序,这将是一个更容易的问题:
app.get('/getMyDatas', async function (req, res) {
try {
let contents = await cloud.collection('contents/').orderBy('date').get();
let tList = [];
for (let cont of contents.docs) {
let users = await cloud.collection('userprofile/').where('userId', '==', cont.data().userId).get();
for (let user of users) {
tList.push({description:cont.data().description, name:user.data().name});
}
}
res.json(tList);
} catch(e) {
console.log(e);
res.sendStatus(500);
}
});
var app = express();
var cloud = firebase.firestore();
app.get('/getMyDatas', function (req, res) {
let tList = [];
let tJson = {};
cloud.collection('contents/').orderBy('date').get().then((contents) => {
contents.docs.forEach(cont=> {
cloud.collection('userprofile/').where('userId', '==', cont.data().userId).get().then((users) => {
users.docs.forEach(user => {
tJson = {description:cont.data().description, name:user.data().name};
tList.push(tJson);
tJson = {};
console.log("LIST IS FILLED SUCCESFULLY : " + JSON.stringify(tList));
});
});
});
console.log(" ??HERE THE LIST IS EMPTY : " + JSON.stringify(tList));
res.json(tList);
});
});
这段代码可以创建我想要的列表。但是我不能在线上使用它 上面写着“res.json(tList)”。
我可以在“console.log('My List : ' + JSON.stringify(tList));"(它正确显示了我的列表。)
res.json(tList) return "[]" 空列表。我怎么能用这个 线?
This code can create the list i want. But i can't use it on the line that says "res.json(tList)".
在尝试使用您在异步操作中构建的结果之前,您没有正确等待异步操作完成。因此,您尝试在添加任何项目之前使用 tList
(时间问题)。
对于任何可能被拒绝的承诺,您也没有任何适当的错误处理。
如果您切换到 for
循环,然后您可以使用 async/await
对您的异步操作进行排序,这将是一个更容易的问题:
app.get('/getMyDatas', async function (req, res) {
try {
let contents = await cloud.collection('contents/').orderBy('date').get();
let tList = [];
for (let cont of contents.docs) {
let users = await cloud.collection('userprofile/').where('userId', '==', cont.data().userId).get();
for (let user of users) {
tList.push({description:cont.data().description, name:user.data().name});
}
}
res.json(tList);
} catch(e) {
console.log(e);
res.sendStatus(500);
}
});