Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID'

流过昼夜 提交于 2021-01-28 03:54:54

问题


I want to get the total count of stars of my github account but when I run my server client flashes an error saying :

... data-increment="{{ metadata[stargazers_ -----------------------^ Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID'

I have used express server, with handlebars for rendering

function parseData(response, user) {
    var checkLang = {};
    const metadata = response.reduce(function(acc, currentItem) {
        acc.stargazers_count += currentItem.stargazers_count;
        if (checkLang[currentItem.language]) {
            checkLang[currentItem.language] = checkLang[currentItem.language] + 1;
        } else {
            checkLang[currentItem.language] = 1;
        }
        return acc;
    }, { stargazers_count: 0 });

    metadata["languages"] = Object.keys(checkLang).map(item => {
        return {
            value: (checkLang[item] / response.length) * 100,
            title: item
        };
    });

    metadata["mainlanguage"] = Object.keys(checkLang).reduce(function(a, b) {
        return checkLang[a] > checkLang[b] ? a : b;
    });

    return metadata;
}

route.get("/:id", function(req, res) {
    axios
        .get("https://api.github.com/users/" + req.params.id)
        .then(user => {
            axios
                .get("https://api.github.com/users/" + req.params.id + "/repos")
                .then(response => {
                    var getMetadata = parseData(response.data, user.data);

                    res.render("private", {
                        response: response.data,
                        user: user.data,
                        metadata: getMetadata
                    });
                })
                .catch((e) => {
                    console.log(e);
                    res.render("404");
                });
            })
            .catch((e) => {
                console.log(e);
                res.render("404");
            });
        });
});

result.hbs

<div
    class="meta-value numscroller"
    data-max="{{ metadata.stargazers_count }}"
    data-min="0"
    data-delay="1"
    data-increment="{{ metadata[stargazers_count] / 10 > 0 ? metadata[stargazers_count] / 10 : 1 }}"
>
    {{ metadata.stargazers_count }}
</div>

I expect it to display count of total stars.


回答1:


The problem is caused by your handlebars template. In the attribute data-increment you are using metadata[stargazers_count], which causes the failure.

Note that it is not advisable to place a lot of logic in your handlebars template. So, to solve your problem I would compute the increment value and add it to your metadata object like this:

function parseData(response, user) {
    var checkLang = {};
    const metadata = response.reduce(function(acc, currentItem) {
        acc.stargazers_count += currentItem.stargazers_count;
        if (checkLang[currentItem.language]) {
            checkLang[currentItem.language] = checkLang[currentItem.language] + 1;
        } else {
            checkLang[currentItem.language] = 1;
        }
        return acc;
    }, { stargazers_count: 0 });

    metadata["languages"] = Object.keys(checkLang).map(item => {
        return {
            value: (checkLang[item] / response.length) * 100,
            title: item
        };
    });

    metadata["mainlanguage"] = Object.keys(checkLang).reduce(function(a, b) {
        return checkLang[a] > checkLang[b] ? a : b;
    });

    metadata.increment = metadata.stargazers_count / 10 > 0 ? metadata.stargazers_count / 10 : 1;

    return metadata;
}

I am adding an increment attribute with the value you were trying to compute in the handlebars template.

Now your handlebars template only needs to use this attribute:

<div
    class="meta-value numscroller"
    data-max="{{ metadata.stargazers_count }}"
    data-min="0"
    data-delay="1"
    data-increment="{{ metadata.increment }}"
>
    {{ metadata.stargazers_count }}
</div>


来源:https://stackoverflow.com/questions/56636426/expecting-id-string-number-boolean-undefined-null-data-got

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!