1

I want to render my json in jade.I m sending Json Data but this give error like this

       app.get('/showRequestAccepted',function(req,res){
      Account.getFriends(req.user,{"friends.status":Status.Accepted},function(err,sonuc)          {
         if(err) throw err
        //Json'i formatted göstermek için  null ve 4 boşluk için
        else
        console.log(sonuc);
        res.render('profil',{sonuc:JSON.stringify(sonuc)});


        });

});

in Jade Using Jade to iterate JSON I find this example this is not help me

         each jsonObj in sonuc
          li=jsonObj.username

I m getting this error

  500 TypeError: /Users/ANTEGRUP/Desktop/passport-local-master/views/profil.jade:31 29|   30| div#userlist > 31| each jsonObj in sonuc 32| li=jsonObj.username 33| 34| Cannot read property 'length' of undefined
Community
  • 1
  • 1
Nazır Dogan
  • 1,494
  • 15
  • 31

1 Answers1

2

I think you are confusing JSON and javascript objects, basically your code should be this:

   app.get('/showRequestAccepted',function(req,res){
   Account.getFriends(req.user,{"friends.status":Status.Accepted},function(err,sonuc) {
     if(err) throw err
     //Json'i formatted göstermek için  null ve 4 boşluk için
     else
       console.log(sonuc);
     res.render('profil',{sonuc: sonuc)});
   });

For your example to work, sonuc must be array of objects with username field in them. In javascript object literal notation this would be an example: [{ username : 'a' }]

Now JSON, is a data interchange format. When you do JSON.stringify(sonuc), you will get a string that represents that array. You can confirm with typeof JSON.stringify(sonuc) === 'string' which returns true. But in this case we need an array of objects, so Array.isArray(sonuc) should return true if it's array.

You might have a look at this question: Javascript object Vs JSON

Community
  • 1
  • 1
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
  • sonuc is returnnig like this [ { _id: 537dbb21407965a10da84630, status: 'accepted', added: Thu May 22 2014 11:54:33 GMT+0300 (EEST), friend: { _id: 537dbb21407965a10da84630, salt: 'f0e95c9e47178f39ef01ca03fa25ab348feb4bc667755715a2f8da7da71436ec', hash: '3d44d3ece69a459b4c9356820565658667f3e4690e2fec821f85fe01aa3a810ea7ffa586c7e74d9c7d16f406352f32abc3b1179ec309026b903a3b5b2611be9b55ea72d3679e67ee7e0d7945c16a58b0f6078f239', username: 'user4', email: 'user4@g.com', __v: 0 } }, – Nazır Dogan May 23 '14 at 09:18
  • In this you should change your code to `li=jsonObj.friend.username` – Farid Nouri Neshat May 23 '14 at 09:23
  • ul 34| > 35| each jsonObj in sonuc 36| li=jsonObj.friend.username 37| 38| Cannot read property 'length' of undefined – Nazır Dogan May 23 '14 at 09:29
  • http://runnable.com/me/U377w3AFeOpZC159 I want to do like this. its almost same but not work in my code – Nazır Dogan May 23 '14 at 09:35
  • runnable giving me 404. – Farid Nouri Neshat May 23 '14 at 09:37
  • http://runnable.com/U38VfqK3uhYNIPSp/loops-and-condition-in-jade-for-node-js .I m sorry try this – Nazır Dogan May 23 '14 at 09:40
  • Worked fine for me, upgrade to the latest jade. – Farid Nouri Neshat May 23 '14 at 09:56
  • this example works but my code is not work :) its so interesting.I upgraded latest jade – Nazır Dogan May 23 '14 at 10:45
  • Then the problem is not the code you have shared. Try remove stuff from your code till you get it working. Then find out which line causes that. – Farid Nouri Neshat May 23 '14 at 11:12