微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从lowdb json 数据库中列出一些带有JS、HTML 的值

如何解决从lowdb json 数据库中列出一些带有JS、HTML 的值

我想要一个 lowdb Json 数据库,在其中我希望能够获取所有“url”并仅列出每个“slug”、“url”和“stats”像这样(使用JS,HTML)-> (下面是它应该如何被列出 {in HTML,JS})

Slug: Example
Url: https://example.com
Stats: 37

Slug: Example1234
Url: https://example.net
Stats: 15

Slug: Exampleio
Url: https://example.io
Stats: 20

这就是数据库的样子:

{
  "urls": [
    {
      "slug": "example","url": "https://example.com","token": "amazingdev1","stats": 37
    },{
      "slug": "example1234","url": "https://example.net","token": "adsfrdmgsrf","stats": 15
    },{
      "slug": "exampleio","url": "https://example.io","token": "07dfpwxukck57rv5","stats": 20
    }
  ]
}

我对所有这些东西都很陌生;你会怎么做? 提前致谢!

解决方法

我找到了问题的答案(感谢@ch1ck3n):

因为 db.json 看起来像这样:

{
  "urls": [
    {
      "slug": "example","url": "https://example.com","token": "ah4ret7w","stats": 37
    },{
      "slug": "example1234","url": "https://example.net","token": "adsfrdmgsrf","stats": 15
    },{
      "slug": "exampleio","url": "https://example.io","token": "07dfpwxukck57rv5","stats": 20
    }
  ]
}

...那么 index.js 将如下所示:

const express = require('express');

const app = express();

const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')

const adapter = new FileSync('db.json')
const db = low(adapter)

re = ""
db.get('urls').__wrapped__.urls.forEach(function(item){
  re += "<div class='card' style='border: 5px solid black'>"
  re += ("Slug: " + item.slug +"<br>");
  re += ("Url: " + item.url+"<br>")
  re += ("Stats: " + item.stats+"<br>")
  re += "</div>"
  re += ("<br>")
});

app.get('/',(req,res) => {
  res.send(re)
});

app.listen(3000,() => {
  console.log('server started');
});

显示了一个工作演示here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。