gpt4 book ai didi

node.js - Jade 中的 Json 解析

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:02 26 4
gpt4 key购买 nike

我想在jade中渲染我的json。我正在发送Json数据,但这会给出这样的错误

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


});

});

Jade Using Jade to iterate JSON我发现这个例子对我没有帮助

         each jsonObj in sonuc
li=jsonObj.username

我收到此错误

  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

最佳答案

我认为你混淆了 JSON 和 javascript 对象,基本上你的代码应该是这样的:

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

为了让您的示例正常工作,sonuc 必须是包含用户名字段的对象数组。在 javascript 对象文字表示法中,这将是一个示例:[{ username : 'a' }]

现在的 JSON 是一种数据交换格式。当您执行 JSON.stringify(sonuc) 时,您将获得表示该数组的字符串。您可以使用返回 true 的 typeof JSON.stringify(sonuc) === 'string' 进行确认。但在本例中,我们需要一个对象数组,因此如果它是数组,则 Array.isArray(sonuc) 应返回 true。

您可能会看一下这个问题:Javascript object Vs JSON

关于node.js - Jade 中的 Json 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23824172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com