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