- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 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.
}
最佳答案
您的 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/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
notificationCenterPublisher = NotificationCenter.default .publisher(for: .NSManagedObjec
我有以下 Rust 代码: #[inline(never)] fn x() -> i32 { 21 } pub fn main() -> i32 { x() } 如果没有优化 (-C
notificationCenterPublisher = NotificationCenter.default .publisher(for: .NSManagedObjec
我有以下 Rust 代码: #[inline(never)] fn x() -> i32 { 21 } pub fn main() -> i32 { x() } 如果没有优化 (-C
假设我的 ASPX 页面没有内联 C# 代码块。 所以,我可以安全地设置 ...在我的 web.config 文件中,不用担心编译错误。 就性能而言,使用以下设置是否会有任何损失? 即“自动”检测
应用程序.js var win1 = Titanium.UI.createWindow({ title:'Tab 1', backgroundColor: 'black', l
基本上,我正在为实现多级优先级队列的 xv6 内核实现一个调度程序。我有一个严重的问题,我不明白,我类(class)的助教不明白,我已经错过了这个项目的最后期限,所以现在帮助我不会给我任何加分 - 但
我想避免 git 自动 merge 我的 composer.json。这样我就可以在 develop 分支中有一个使用 dev-develop 包的 composer.json,而 master 中的
当比较两种不同的算法实现时(因此,不关心它们的绝对性能,而只关心相对性能)我是否最好强制 Java 只运行解释代码? 也就是说,打开 -Xint 标志会更好吗? 最佳答案 我不认为禁用 JIT 会更好
class A{ const size_t number; public: A(size_t number): number(number) {} void f(){
问题 寻找在以下之间切换的方法: 总是 从不 仅在静默模式下 仅当不处于静默模式时 这些选项在手机上的路径---菜单>>设置>>声音>>振动---找到。 通过手机导航很容易更改(顺便说一句,我的手机是
如何设置电源设置关闭:从不(关闭显示器=从不,让计算机进入休眠状态=从不),通过c#代码 最佳答案 这个问题中给出的链接可以告诉你一个方法。 Programmatically change Windo
我是一名优秀的程序员,十分优秀!