0
我新手,Redis的和的NodeJS,我看過this教程,我想按名稱搜索用戶,如何在Redis中的對象內搜索值?
這裏是對象,從Redis的返回,當我通過ID:
{
first_name: 'john',
last_name: 'doe',
email: '[email protected]',
phone: '543313305',
id: 'user001'
}
下面是搜索的功能:
app.post('/user/search',function (req,res,next) {
let id = req.body.id;
client.hgetall(id ,function(err,obj){
if(!obj){
res.render('searchusers',{
error:"user doesn't exist",
});
} else {
obj.id = id
console.log(obj);
res.render('details',{
user:obj, });
}
});
});
我試圖以取代ID搜索由FIRST_NAME通過這樣的搜索方式:
首先,我已經改變的字段名 「FIRST_NAME」 而不是 「ID」
<h1>Search Users</h1>
{{#if error}} <span>{{error}}</span>{{/if}}
<form class="form-inline" method="POST" action="/user/search">
<div class="form-group">
<input type="text" name="first_name" placeholder="Search" class="form-
control">
</div>
<input type="submit" class="btn btn-primary" value="Search">
而且比我在app.js改變了它;
app.post('/user/search',function (req,res,next) {
let first_name = req.body.first_name;
client.hgetall(first_name ,function(err,obj){
if(!obj){
res.render('searchusers',{
error:"user doesn't exist",
});
} else {
obj.first_name = first_name
console.log(obj);
res.render('details',{
user:obj, });
}
});
});