试图获取正确的json数据到Page。 JavaScript Node.Js

时间:2019-01-09 19:01:57

标签: javascript node.js

app.get('/book/:bibleBook/:bibleChapter/:bibleVerse', (req, res) => {
    const book = req.params.bibleBook;
    const chapter = req.params.bibleChapter;
    const verse = req.params.bibleVerse;
    const bibleVerse = [
        {
          "id": 1001001,
          "Book": "Genesis",
          "Chapter": 1,
          "Verse": 1,
          "Text": "In the beginning God created the heaven and the earth."
        },
        {
          "id": 1001002,
          "Book": "Genesis",
          "Chapter": 1,
          "Verse": 2,
          "Text": "And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."
        },
        {
          "id": 1001003,
          "Book": "Genesis",
          "Chapter": 1,
          "Verse": 3,
          "Text": "And God said, Let there be light: and there was light."
        },
        {
          "id": 1001004,
          "Book": "Genesis",
          "Chapter": 1,
          "Verse": 4,
          "Text": "And God saw the light, that it was good: and God divided the light from the darkness."
        },
        {
          "id": 1001005,
          "Book": "Genesis",
          "Chapter": 1,
          "Verse": 5,
          "Text": "And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."
        }
    ]
    res.send('This is ' + book + ' ' + chapter + ':' + verse);
});

晚上好。我正在网站上。我正在尝试从页面下面的文件中发布json数据。我为这节经文写了一个网址。在页面上,我得到书,章节和经文。有人能向我解释一下如何从正确页面上的bibelVerse获取文本。

1 个答案:

答案 0 :(得分:0)

如果您只想返回经文,则需要编写一些内容以在数组中找到正确的经文。所以我会做这样的事情...

let foundVerse = bibleVerse.find(function(verseEl) {
    return verseEl.book === book && verseEl.chapter === chapter && verseEl.verse === verse;
});

您可能还需要验证从req.params获得的这些值,以确保您拥有有效的数据。

这时,您将拥有匹配的项目或null的{​​{1}}对象。因此,您可以编写一些逻辑以发送回正确的响应(我假设是文本?)。

foundVerse