javascript - 如何获取包含阿根廷等国籍的演员按全名排序:输入网址邮递员时出错
问题描述
我只想获取包含国籍的演员,例如按全名排序的“阿根廷”,以便升序。
这是我的演员 Routes.js:
router.get("/ask/actorsask", actorsCtrl.getActorByNationality);
这是我的演员 controller.js
export const getActorByNationality = async (req,res) =>{
try{
var regExpTerm = new RegExp(req.query.ask,'i');
var regExpSearch =
[{
nationality: {$regex: regExpTerm}
}];
const actors = await Actor.find({$or: regExpSearch})
.where('nationality').equals({$or: regExpSearch}).sort({fullName: 1})
.exec(callback);
res.status(200).send(actors);
}catch (e) {
res.status(500).send({ mensaje: e });
}
};
这是我的演员的 model.js:
const actorSchema = new Schema({
fullName: String,
birthDate: Date,
genre: String,
nationality: String
},
{
timestamps:{required: true},
versionKey: false
}
)
预期的输出应该是这样的:
[ {
"_id": "6004b4323265a91b18f96d39",
"fullName": "Ana Lopez",
"birthDate": "1993-02-10T17:00:00.000Z",
"genre": "female",
"nationality": "argentine",
"createdAt": "2021-01-17T22:03:30.011Z",
"updatedAt": "2021-01-17T22:03:30.011Z"
},
{
"_id": "6004b348b1cd7f28c8d5569d",
"fullName": "Omar Aparicio",
"birthDate": "1994-08-10T14:00:00.032Z",
"genre": "male",
"nationality": "argentine",
"createdAt": "2021-01-17T21:59:36.185Z",
"updatedAt": "2021-01-17T21:59:36.185Z"
},
]
但真正的输出是这个:
[
{
"_id": "6004b348b1cd7f28c8d5569d",
"fullName": "Omar Aparicio",
"birthDate": "1994-08-10T14:00:00.032Z",
"genre": "male",
"nationality": "argentine",
"createdAt": "2021-01-17T21:59:36.185Z",
"updatedAt": "2021-01-17T21:59:36.185Z"
},
{
"_id": "6004b4323265a91b18f96d39",
"fullName": "Ana Lopez",
"birthDate": "1993-02-10T17:00:00.000Z",
"genre": "female",
"nationality": "argentine",
"createdAt": "2021-01-17T22:03:30.011Z",
"updatedAt": "2021-01-17T22:03:30.011Z"
},
{
"_id": "6004b4423265a91b18f96d3a",
"fullName": "nishimura sam",
"birthDate": "1978-09-22T16:00:00.000Z",
"genre": "male",
"nationality": "albanese",
"createdAt": "2021-01-17T22:03:46.143Z",
"updatedAt": "2021-01-17T22:13:20.411Z"
}
]
我在邮递员中将其输入为 url,选择 GET 方法:
http://localhost:4000/api/actors/ask/actorsask?nationality="argentine"
我认为问题出在 getActorByNationality 方法中的 actor routes.js 或 actor controller.js 上,但我不知道如何解决。
怎么了?
解决方案
看起来有一些来自另一个来源的代码副本。我修复了一些问题。试试这个版本。
export const getActorByNationality = async (req,res) =>{
try{
const findCond = req.query.nationality? {nationality: req.query.nationality}: {}
const actors = await Actor.find(findCond)
.sort({fullName: 1})
.exec(callback);
res.status(200).send(actors);
}catch (e) {
res.status(500).send({ mensaje: e });
}
};
推荐阅读
- ruby-on-rails - Rails 重置密码电子邮件未触发
- html - CSS 文本幻灯片
- excel - 带有两个 y 轴和分割区域的 Excel 条形图
- maven - cannot download snapshot from nexus
- ruby-on-rails - rails controller's action / disable concurrency
- javascript - 回调函数缺少所需数据的范围
- python - ndarray object to array conversion, where object contains variable size images
- php - Laravel application on sub-directory
- docker - 400 Bad Request nginx/1.13.12
- php - How to make access denied for accessing image via browser url in magento2?