简体   繁体   English

为什么我的服务器无法完全读取我的json文件?

[英]Why won't my server read my json file completely?

I made a get request in a function for fav.json and programmed the id="fav_button" button to run the function, and I set up an express server. 我在fav.json的函数中发出了get请求,并编程了id =“ fav_button”按钮来运行该函数,然后设置了一个快速服务器。 However, when I click the button, it only displays the last item in the json file. 但是,当我单击按钮时,它仅显示json文件中的最后一项。 The json file being fetched is an array of objects. 正在获取的json文件是对象数组。

I have tried using .send instead of .json as well as .sendFile. 我尝试使用.send而不是.json以及.sendFile。 I have tried debugging the json file, but there are no issues. 我已经尝试调试json文件,但是没有问题。 I have tried using jquery instead, but it makes no difference. 我尝试使用jquery代替,但没有区别。

// script.js
function get_fav(e) {
    if (!e) {
        e = window.event;}
    el = e.target || e.srcElement;
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        if (xhr.status === 200) {
            let list = JSON.parse(xhr.response);
            for (var i in list) {
                let hold = fav_tem;
                hold = hold.replace('%title', list[i].song);
                hold = hold.replace('%artist', list[i].artist);
                hold = hold.replace('%mood', list[i].mood);
                document.getElementById('faves_field').innerHTML = hold;}}};
    xhr.open('GET', 'fav.json', true);
    xhr.send();}
const fav_el = document.getElementById('fav_button');
if (fav_el.addEventListener) {
    fav_el.addEventListener('click', get_fav, false);}
else {fav_el.onclick = get_fav;}...


// app.js
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const fs = require('fs');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res) {
    res.send(fs.readFileSync('public/index.html'));
});

app.get('public/fav.json', function(req, res) {
    res.json(fs.readFileSync('public/fav.json'));
});...

...app.listen(3003, function() {
       console.log('Server started on port 3003...');
   });

My script is supposed to request fav.json and append each part of the object to the blue boxes like this: (in bold) Song - artist. 我的脚本应该请求fav.json并将对象的每个部分附加到蓝色框,如下所示:(以粗体显示)Song-artist。 Mood: mood(s). 情绪:情绪。 My app is supposed to listen for the request for public/fav.json, then send it. 我的应用程序应该侦听对public / fav.json的请求,然后将其发送。

However, for some reason, when I click the allocated button, it only displays the last object of the json file in the blue box below. 但是,由于某种原因,当我单击分配的按钮时,它仅在下面的蓝色框中显示json文件的最后一个对象。 Here's a link to the full project (I am not very experienced with web developement): https://github.com/YungLonk/Practice-Node-JS . 这是整个项目的链接(我对Web开发不是很有经验): https : //github.com/YungLonk/Practice-Node-JS What am I missing or doing wrong? 我想念什么或做错什么?

You're looping over the list, and then setting the html. 您正在遍历列表,然后设置html。 This is always going to set it to the last item in list. 总是将其设置为列表中的最后一项。

        for (var i in list) {
            let hold = fav_tem;
            hold = hold.replace('%title', list[i].song);
            hold = hold.replace('%artist', list[i].artist);
            hold = hold.replace('%mood', list[i].mood);
            document.getElementById('faves_field').innerHTML = hold;
        };

You should put the variable that holds the data and the html property outside of the loop 您应该将包含数据的变量和html属性放在循环外部

    let hold = fav_tem;
    for (var i in list) {
        hold = hold.replace('%title', list[i].song);
        hold = hold.replace('%artist', list[i].artist);
        hold = hold.replace('%mood', list[i].mood);
    };

    document.getElementById('faves_field').innerHTML = hold;

EDIT: 编辑:

I have just had a quick look over your project and I see that fav_tem has only one item (in my head I was thinking that it contains a number of them). 我对您的项目进行了快速浏览,发现fav_tem仅包含一项(在我看来,我认为其中包含许多项)。 So once you do the replace, then that's it. 因此,一旦完成替换,就完成了。 It can't replace it again because the text is no longer there. 它不能再次替换它,因为文本不再存在。

So in that case you would want to append. 因此,在这种情况下,您将要追加。 See below: 见下文:

    let favitems = "";
    for (var i in list) {
        let hold = fav_tem;
        hold = hold.replace('%title', list[i].song);
        hold = hold.replace('%artist', list[i].artist);
        hold = hold.replace('%mood', list[i].mood); 
        favitems = favitems + "<br />" + hold;
    };

    document.getElementById('faves_field').innerHTML = favitems;

Let me know how that goes, I believe that is what you're looking for. 让我知道这是怎么回事,我相信这就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM