gpt4 book ai didi

node.js - Express API Endpoint 从不返回数据。 Sequelize

转载 作者:行者123 更新时间:2023-12-03 22:28:04 25 4
gpt4 key购买 nike

我有一个使用 Sequelize 作为 ORM 的应用程序。我有几个模型运行良好,但我还需要从另一个模型中提取信息以获得过滤列表。此端点 Controller 的模型只是协助建议实体

  fiveFurthestInsiders(req, res) {
let targetArea = req.params.areaId;
//Set the area we wish to set a match for...
let gender = req.params.gender;
//matches must be of the same gender.
let cty = req.params.country;
//and from the same country
let lang = req.params.language;
//must also speak the same language
let distances = [];
//holds the distance calculations. (calculated for each request.)
let returnUsers = [];
//holds the suggestions to return to the F.E.
return Area.findAll()
//get all the areas and Geo Codes
.then(AreaList => {
AreaList.forEach(x => {
//foreach Area of the Area List
distances.push({
//add to the distance array
target: targetArea,
//area being matched
source: x,
//area for this index
distance: areaController.calcDist(targetArea, x),
//distance from target to x (KM).
country: areaController.determineCountry(targetArea)
//determine if country for target.
});
});
}).then(_ => {
distances.sort((x, y) => {
//sort the array Descending.
return x.distance - y.distance;
}).filter(x => x.country === cty);
//filter out other countries
}).then(x => {
if (distances.indexOf(x) < 10) {
//cut out all but the 10 furthest areas for limiting purposes
return x;
}
}).then(x => {
distances.forEach(y => {
//foreach item in the distance array.
Outsider.findAll({
//find the first two people
where: {
areaCode: y.target,
//that are in the far area
gender: gender,
// are of the same gender
language: lang,
//speak the same language
country: cty
//and are in the same country
},
limit: 2
}).then(insiders => {
returnUsers.push(insiders);
//add the people from the query to the return list.
})
})
})
returnUsers = returnUsers.splice(0, 10);
//Cut down the array again as some areas might
//have had more or less people to add.
res.status(200).send(returnUsers);
//send the array of people.
}

一切似乎都在工作,但它永远不会返回任何东西给 Postman。

我的逻辑有问题吗?

最佳答案

您的 Sequelize 数据库查询是 Promise ,这意味着它是一个异步过程。考虑将您的 res.send 移至最后一个,然后如下所示

  fiveFurthestInsiders(req, res) {
let targetArea = req.params.areaId;
//Set the area we wish to set a match for...
let gender = req.params.gender;
//matches must be of the same gender.
let cty = req.params.country;
//and from the same country
let lang = req.params.language;
//must also speak the same language
let distances = [];
//holds the distance calculations. (calculated for each request.)
let returnUsers = [];
//holds the suggestions to return to the F.E.
return Area.findAll()
//get all the areas and Geo Codes
.then(AreaList => {
AreaList.forEach(x => {
//foreach Area of the Area List
distances.push({
//add to the distance array
target: targetArea,
//area being matched
source: x,
//area for this index
distance: areaController.calcDist(targetArea, x),
//distance from target to x (KM).
country: areaController.determineCountry(targetArea)
//determine if country for target.
});
});
}).then(_ => {
distances.sort((x, y) => {
//sort the array Descending.
return x.distance - y.distance;
}).filter(x => x.country === cty);
//filter out other countries
}).then(x => {
if (distances.indexOf(x) < 10) {
//cut out all but the 10 furthest areas for limiting purposes
return x;
}
}).then(x => {
distances.forEach(y => {
//foreach item in the distance array.
Outsider.findAll({
//find the first two people
where: {
areaCode: y.target,
//that are in the far area
gender: gender,
// are of the same gender
language: lang,
//speak the same language
country: cty
//and are in the same country
},
limit: 2
}).then(insiders => {
returnUsers.push(insiders);
//add the people from the query to the return list.
returnUsers = returnUsers.splice(0, 10);
//Cut down the array again as some areas might
//have had more or less people to add.
res.status(200).send(returnUsers);
//send the array of people.
})
})
})

}

关于node.js - Express API Endpoint 从不返回数据。 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48083561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com