修改一个查询参数:express JS get request
Modify a query parameter: express JS get request
我正在从 angular 组件向 Express 服务器发送请求。一切正常(我得到了预期的结果,并且显示正确)。
当我尝试修改QUERY 参数时,没有显示任何内容。例如,假设用户搜索单词:'hands'。我想标记这个词,使其成为单数。
这是服务器端实现:
app.get('/api/words', function(req, res) {
var name = req.query.name;
var option = req.query.option;
// english word:
if(option == 1){
// This is where I check if the word is plural to turn it into singular form
if(name[name.length - 1]== 's'){
name = name.slice(0, name.length - 1);
}
fs.readFile('words.xml', 'utf-8', function (err, data){
if(err){
} else{
parser.parseString(data, function (err, result) {
let words = result['entry']['form'];
str= words.filter(x => x.orth == name);
}
res.send(str);
调用http服务的angular组件是:
getWordList(name: string){
this.spelling = name.toString().toLowerCase();
this.webservice.getWords(this.spelling, this.selected)
.subscribe((res: Array<Word>)=> {
this.elements = res.filter(d=> d.orth == this.spelling || d.asuddimIsem == this.spelling);
this.dataLoaded.emit(this.elements);
this.webservice.setData(this.elements);
this.router.navigate(['/words']);
})
}
数据是一个包含英文单词列表的 xml 文件。查询搜索单词的单数形式。因此,如果它是一个复数词,标记化应该检索正确的项目。
例如,当我使用 'hands' 发送请求时,控制台显示状态代码 200,并有正确的响应。然而什么也没有显示。
在 angular 中,您使用 this.spelling
过滤结果,据我了解,this.spelling
包含您搜索的词(即 hands
)和 d.orth
包含单数形式。
从那时起,过滤将删除客户端的每个元素。
如何正确解决这个问题取决于您的用例。例如。在客户端过滤或删除客户端过滤的单数,因为您已经在服务器端进行了过滤。
我正在从 angular 组件向 Express 服务器发送请求。一切正常(我得到了预期的结果,并且显示正确)。
当我尝试修改QUERY 参数时,没有显示任何内容。例如,假设用户搜索单词:'hands'。我想标记这个词,使其成为单数。
这是服务器端实现:
app.get('/api/words', function(req, res) {
var name = req.query.name;
var option = req.query.option;
// english word:
if(option == 1){
// This is where I check if the word is plural to turn it into singular form
if(name[name.length - 1]== 's'){
name = name.slice(0, name.length - 1);
}
fs.readFile('words.xml', 'utf-8', function (err, data){
if(err){
} else{
parser.parseString(data, function (err, result) {
let words = result['entry']['form'];
str= words.filter(x => x.orth == name);
}
res.send(str);
调用http服务的angular组件是:
getWordList(name: string){
this.spelling = name.toString().toLowerCase();
this.webservice.getWords(this.spelling, this.selected)
.subscribe((res: Array<Word>)=> {
this.elements = res.filter(d=> d.orth == this.spelling || d.asuddimIsem == this.spelling);
this.dataLoaded.emit(this.elements);
this.webservice.setData(this.elements);
this.router.navigate(['/words']);
})
}
数据是一个包含英文单词列表的 xml 文件。查询搜索单词的单数形式。因此,如果它是一个复数词,标记化应该检索正确的项目。
例如,当我使用 'hands' 发送请求时,控制台显示状态代码 200,并有正确的响应。然而什么也没有显示。
在 angular 中,您使用 this.spelling
过滤结果,据我了解,this.spelling
包含您搜索的词(即 hands
)和 d.orth
包含单数形式。
从那时起,过滤将删除客户端的每个元素。
如何正确解决这个问题取决于您的用例。例如。在客户端过滤或删除客户端过滤的单数,因为您已经在服务器端进行了过滤。