我可以在一个 res 状态下发送所有模式吗?
Can I possible send the all schema in one res status?
因此,我正在尝试试验如何获取我的 5 个架构对象中的所有项目。这是它的样子。
const GetAllApple = (req,res) => {
const list = []
Ipad.find()
.then(data => list.push(data))
.catch(err => console.log(err))
Mac.find()
.then(data => list.push(data))
Accessories.find()
.then(data => list.push(data))
.catch(err => console.log(err))
HomeTools.find()
.then(data => list.push(data))
Phone.find()
.then(data => list.push(data))
.catch(err => console.log(err))
console.log(list)
res.send(list)
}
但它不起作用,它给了我一个空数组,在我的 postman
中也给出了一个空输出,但它没有给出任何错误。
但是当我尝试做这件事时,我注释掉了 Ipad, Mac, HomeTools and Phone
我的 Accessories should something like this
和 console.log(列表)
Accessories.find()
.then(data =>
{
list.push(data);
res.send(data)
})
.catch(err => console.log(err))
console.log(list)
终端中的输出仍然是空列表[]
但在我的邮递员中
[
{
"_id": "624da84b97cdc2714b8fd5f5",
"itemname": "ACBASDASD",
"itemno": 122,
"image": "123123.jpg",
"createdAt": "2022-04-06T14:48:43.515Z",
"updatedAt": "2022-04-06T14:48:43.515Z",
"__v": 0
},
{
"_id": "624dac788a77d5ea59650f1e",
"itemname": "ACBASDASD",
"itemno": 122,
"image": "gray.jpg",
"createdAt": "2022-04-06T15:06:32.173Z",
"updatedAt": "2022-04-06T15:06:32.173Z",
"__v": 0
}
]
是这样的..有人有想法吗?我想试试这个,也许还有另一个或任何你可能想到的。我知道这对我来说也有点奇怪,但我想尝试这个新的不同想法,哈哈,但效果不是很好。
已编辑
我也试过这样的东西
Ipad.find()
.then(data => res.send(data))
.catch(err => console.log(err))
Mac.find()
.then(data => res.send(data))
.catch(err => console.log(err))
Accessories.find()
.then(data => res.send(data))
.catch(err => console.log(err))
HomeTools.find()
.then(data => res.send(data))
.catch(err => console.log(err))
Phone.find()
.then(data => res.send(data))
.catch(err => console.log(err))
但这感觉不合法,因为我多次调用 res 回调。
您可能需要反序列化您的响应对象
Ipad.find()
.then(res => {
const data = res.json();
list.push(data);
})
.catch(err => console.log(err))
您可以使用承诺链、Promise.all 语法或 async/await
语法:
承诺链:
const GetAllApple = (req, res) => {
const list = []
Ipad.find()
.then(data => {
list.push(data)
return Mac.find()
})
.then(data => {
list.push(data)
return Accessories.find()
})
// ...
.then(data => {
list.push(data)
console.log(list)
res.send(list)
})
.catch(err => {
console.log(err)
res.status(500).send(err)
})
}
Promise.all
const GetAllApple = (req, res) => {
Promise.all([
Ipad.find(),
Mac.find(),
// ...
])
.then(list => {
console.log(list)
res.send(list)
})
.catch(err => {
console.log(err)
res.status(500).send(err)
})
}
async/await
const GetAllApple = async (req, res) => { // async function
try {
const ipads = await Ipad.find() // await
const macs = await Mac.find()
// ...
res.send([ipads, macs]) // or res.send([...ipads, ...macs])
} catch(err) {
console.log(err)
res.status(500).send(err)
}
}
因此,我正在尝试试验如何获取我的 5 个架构对象中的所有项目。这是它的样子。
const GetAllApple = (req,res) => {
const list = []
Ipad.find()
.then(data => list.push(data))
.catch(err => console.log(err))
Mac.find()
.then(data => list.push(data))
Accessories.find()
.then(data => list.push(data))
.catch(err => console.log(err))
HomeTools.find()
.then(data => list.push(data))
Phone.find()
.then(data => list.push(data))
.catch(err => console.log(err))
console.log(list)
res.send(list)
}
但它不起作用,它给了我一个空数组,在我的 postman
中也给出了一个空输出,但它没有给出任何错误。
但是当我尝试做这件事时,我注释掉了 Ipad, Mac, HomeTools and Phone
我的 Accessories should something like this
和 console.log(列表)
Accessories.find()
.then(data =>
{
list.push(data);
res.send(data)
})
.catch(err => console.log(err))
console.log(list)
终端中的输出仍然是空列表[]
但在我的邮递员中
[
{
"_id": "624da84b97cdc2714b8fd5f5",
"itemname": "ACBASDASD",
"itemno": 122,
"image": "123123.jpg",
"createdAt": "2022-04-06T14:48:43.515Z",
"updatedAt": "2022-04-06T14:48:43.515Z",
"__v": 0
},
{
"_id": "624dac788a77d5ea59650f1e",
"itemname": "ACBASDASD",
"itemno": 122,
"image": "gray.jpg",
"createdAt": "2022-04-06T15:06:32.173Z",
"updatedAt": "2022-04-06T15:06:32.173Z",
"__v": 0
}
]
是这样的..有人有想法吗?我想试试这个,也许还有另一个或任何你可能想到的。我知道这对我来说也有点奇怪,但我想尝试这个新的不同想法,哈哈,但效果不是很好。
已编辑 我也试过这样的东西
Ipad.find()
.then(data => res.send(data))
.catch(err => console.log(err))
Mac.find()
.then(data => res.send(data))
.catch(err => console.log(err))
Accessories.find()
.then(data => res.send(data))
.catch(err => console.log(err))
HomeTools.find()
.then(data => res.send(data))
.catch(err => console.log(err))
Phone.find()
.then(data => res.send(data))
.catch(err => console.log(err))
但这感觉不合法,因为我多次调用 res 回调。
您可能需要反序列化您的响应对象
Ipad.find()
.then(res => {
const data = res.json();
list.push(data);
})
.catch(err => console.log(err))
您可以使用承诺链、Promise.all 语法或 async/await
语法:
承诺链:
const GetAllApple = (req, res) => {
const list = []
Ipad.find()
.then(data => {
list.push(data)
return Mac.find()
})
.then(data => {
list.push(data)
return Accessories.find()
})
// ...
.then(data => {
list.push(data)
console.log(list)
res.send(list)
})
.catch(err => {
console.log(err)
res.status(500).send(err)
})
}
Promise.all
const GetAllApple = (req, res) => {
Promise.all([
Ipad.find(),
Mac.find(),
// ...
])
.then(list => {
console.log(list)
res.send(list)
})
.catch(err => {
console.log(err)
res.status(500).send(err)
})
}
async/await
const GetAllApple = async (req, res) => { // async function
try {
const ipads = await Ipad.find() // await
const macs = await Mac.find()
// ...
res.send([ipads, macs]) // or res.send([...ipads, ...macs])
} catch(err) {
console.log(err)
res.status(500).send(err)
}
}