Sunday 28 May 2017

How to get value from Redis object?

I'm newbie to Redis and Nodejs, I've watched this tutorial and i'm trying to search users by name,

Here is the object, returned from redis when i pass the id:

  {
    first_name: '‪john',
    last_name: 'doe',
    email: 'john@gmail.com',
    phone: '543313305',
    id: 'user001' 
   } 

Here is the function of the search :

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, });
    }
   });
});

I've tried to replace the search by id to search by first_name by doing this:

First I've changed the field name to "first_name" instead of "id"

<h1>Search Users</h1>
 <span></span>
 <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">

And than I've changed it in the 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, });
    }
   });
});



via RoyBarOn

No comments:

Post a Comment