gpt4 book ai didi

node.js - 如何使用 node.js/mongodb 在 HTML 中显示任意的无模式数据

转载 作者:可可西里 更新时间:2023-11-01 09:37:30 26 4
gpt4 key购买 nike

我正在使用 mongodb 将应用程序错误日志存储为 json 文档。我希望能够将错误日志格式化为 HTML,而不是将纯 json 返回给浏览器。日志完全是无模式的——它们可以随时更改,所以尝试这样做是没有用的(在 Jade 中):

    - var items = jsonResults

- each item in items
h3 Server alias: #{item.ServerAlias}
p UUID: #{item.UUID}
p Stack trace: #{item.StackTrace}
h3 Session: #{item.Session}
p URL token: #{item.Session.UrlToken}
p Session messages: #{item.Session.SessionMessages}

因为我不知道 JSON 结构中实际会提前发生什么。我想要的肯定是可能的,但是?我正在阅读的所有内容都说该模式不是由数据库强制执行的,但是您的 View 代码无论如何都会概述您的模式 - 但我们有数百个可能的字段可以随时删除或添加,因此管理 View 这种方式是相当难以管理的。

我错过了什么?我是否对技术做出了错误的假设?走错路了吗?


使用评论后的额外信息进行编辑:

json 文档看起来像这样

{
"ServerAlias":"GBIZ-WEB",
"Session":{
"urltoken":"CFID=10989&CFTOKEN=f07fe950-53926E3B-F33A-093D-3FCEFB&jsessionid=84303d29a229d1",
"captcha":{

},
"sessionmessages":{

},
"sessionid":"84197a667053f63433672873j377e7d379101"
},
"UUID":"53934LBB-DB8F-79T6-C03937JD84HB864A338",
"Template":"\/home\/vagrant\/dev\/websites\/g-bis\/code\/webroot\/page\/home\/home.cfm, line 3",
"Error":{
"GeneratedContent":"",
"Mailto":"",
"RootCause":{
"Message":"Unknown tag: cfincflude.",
"tagName":"cfincflude",
"TagContext":[
{
"RAW_TRACE":"\tat cfhome2ecfm1296628853.runPage(\/home\/vagrant\/dev\/websites\/nig-bis\/code\/webroot\/page\/home\/home.cfm:3)",
"ID":"CFINCLUDE",
"TEMPLATE":"\/home\/vagrant\/dev\/websites\/nig-bis\/code\/webroot\/page\/home\/home.cfm",
"LINE":3,
"TYPE":"CFML",
"COLUMN":0
},
{
"RAW_TRACE":"\tat cfdisplay2ecfm1093821753.runPage(\/home\/vagrant\/dev\/websites\/nig-bis\/code\/webroot\/page\/display.cfm:6)",
"ID":"CFINCLUDE",
"TEMPLATE":"\/home\/vagrant\/dev\/websites\/nig-bis\/code\/webroot\/page\/display.cfm",
"LINE":6,
"TYPE":"CFML",
"COLUMN":0
}
]
}
}

...等等,但可能会根据生成日志的各个项目配置为触发的内容而改变。

我最终想要的是一个格式化的 HTML 页面,其中包含下面列出的每个父项和子项的标题,并在数据结构中迭代。上面的 Jade 示例实际上是我们需要输出的内容,但没有在 View 中进行硬编码。

Mike 在评论中对问题的分析是,从一堆没有太多共同点的集合中创建一个类似表的结构是正确的。数据是相关的,但仅在单个文档中——因此将模式硬编码为任何内容几乎是不可能的,因为它需要您首先知道数据结构是什么样的。

最佳答案

基本思想是@Gates VP 所描述的。我用 underscore.js遍历数组/对象。

function formatLog(obj){
var log = "";
_.each(obj, function(val, key){
if(typeof(val) === "object" || typeof(val) === "array"){
// if we have a new list
log += "<ul>";
log += formatLog(val);
log += "</ul>";
}
else{
// if we are at an endpoint
log += "<li>";
log += (key + ": " + val);
log += "</li>";
}
});
return log;
}

如果您对您提供的示例数据调用 formatLog(),它会返回

  • 服务器别名:GBIZ-WEB
    • urltoken:CFID=10989&CFTOKEN=f07fe950-53926E3B-F33A-093D-3FCEFB&jsessionid=84303d29a229d1
        • /ul>
        • session ID:84197a667053f63433672873j377e7d379101
      • UUID:53934LBB-DB8F-79T6-C03937JD84HB864A338
      • 模板:/home/vagrant/dev/websites/g-bis/code/webroot/page/home/home.cfm,第 3 行
        • GeneratedContent:
        • Mailto:
          • Message:未知标签: cfincflude.
          • tagName: cfincflude
              • RAW_TRACE: 在 cfhome2ecfm1296628853.runPage(/home/vagrant/dev/websites/nig-bis/code/webroot/page/home/home.cfm:3)
              • ID:CFINCLUDE
              • 模板:/home/vagrant/dev/websites/nig-bis/code/webroot/page/home/home.cfm
              • LINE: 3
              • TYPE: CFML
              • COLUMN: 0
              • RAW_TRACE: 在cfdisplay2ecfm1093821753.runPage(/home/vagrant/dev/websites/nig-bis/code/webroot/page/display.cfm:6)
              • ID: CFINCLUDE
              • TEMPLATE:/home/vagrant/dev/websites/nig-bis/code/webroot/page/display .cfm
              • 行:6
              • 类型:CFML
              • 列:0

      如何格式化就看你的了。

    关于node.js - 如何使用 node.js/mongodb 在 HTML 中显示任意的无模式数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11557471/

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