期望'ID','STRING','NUMBER','BOOLEAN','UNDEFINED','NULL','DATA',得到'INVALID' [英] Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID'

查看:66
本文介绍了期望'ID','STRING','NUMBER','BOOLEAN','UNDEFINED','NULL','DATA',得到'INVALID'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我的github帐户的星星总数,但是当我运行服务器客户端时,会闪烁一条错误消息:

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 ="{{元数据[stargazers_----------------------- ^期望'ID','STRING','NUMBER','BOOLEAN','UNDEFINED','NULL','DATA','INVALID'

... 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.

推荐答案

问题是由您的车把模板引起的.在属性 data-increment 中,您使用的是 metadata [stargazers_count] ,这会导致失败.

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

请注意,建议不要在车把模板中放置很多逻辑.因此,要解决您的问题,我将计算 increment 值并将其添加到您的 metadata 对象中,如下所示:

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;
}

我要添加一个 increment 属性,其中包含您要在车把模板中尝试计算的值.

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>

这篇关于期望'ID','STRING','NUMBER','BOOLEAN','UNDEFINED','NULL','DATA',得到'INVALID'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆