将变量从车把传递到Node.js中的JavaScript

时间:2019-08-20 00:45:50

标签: javascript node.js express handlebars.js

我正在使用nodejs制作仪表板,并且在理解如何将变量从车把传递到脚本中时遇到一些麻烦。

这是我的file.js代码:

//Servershizzle

var path = require('path');

var express = require('express');

var router = express.Router();

// Ungenutzte Variablen

//var timestamp = [];

//var date = [];

//var WikiRows = [];

//var TimestampSort = [];

//var group_id = [];

//var user_id = [];

//var type = [];

//var results = [];

//var compare = [];

// Für Funktion und Diff benötigt:

var Diff = require('text-diff');

var diff = new Diff(); // options may be passed to constructor; see below

const csv = require('csv-parser');

const fs = require('fs');

var zieltype = [];

var zieltimestamp = [];

var zielgroup_id = [];

var zieluser_id = [];

// Test-Params:

zieltype = 'wiki';

zieltimestampe = '1540383631';

zielgroup_id = '154';

zieluser_id = 'A1pSmv1agwPDvi+ITOoENw==';

//Ausführung für diesen Seitentest:

// Code:

//Funktion um nach Type (Wiki) zu filtern, Variable über "zieltype"

function filterWikiEntries(value) {

    return value.type === zieltype;

}

//Funktion um nach Timestamp zu filtern, Variable über "zieltimestamp"

function filterTimeStamp(value) {

    return value.timestamp === zieltimestamp;

}

//Funktion um nach Group_ID zu filtern, Variable über "zielgroup_id"

function filterGroupID(value) {

    return value.group_id === zielgroup_id;

}

//Funktion um nach User_ID zu filtern, Variable über "zieluser_id"

function filterUserID(value) {

    return value.user_id === zieluser_id;

}

//Initialisierung von Variablen

var auswahl = [];

var results = [];

const filename = path.join(__dirname, "../../data/IKARion_CVK_Course_T1_T6.csv");

fs.createReadStream(filename)

    .pipe(csv({separator: ";"}))

    .on('data', (data) => results.push(data))

    .on('end', () => {

        auswahl = results.filter(filterWikiEntries);

        auswahl = auswahl.filter(filterGroupID);

        auswahl = auswahl.filter(filterUserID);

        var current = auswahl.filter(filterTimeStamp);

        results = results.filter(filterWikiEntries);

        results = results.filter(filterGroupID);

        var index;

        for (var i = 0; i < results.length; i++) {

            if (results[i].timestamp == current[0].timestamp) {

                index = i;

            }

            results[i].timestamp = new Date(results[i].timestamp * 1000);

        }

        if (index == 0) {

            var text1 = '';

        } else {

            var text1 = results[i - 1].content;

        }

        var text2 = current[0].content;

        var textDiff = diff.main(text2, text1); // produces diff array

        results[index].compare = diff.prettyHtml(textDiff); // produces a formatted HTML string

    });

router.get('/', function (req, res, next) {

    res.render('spielwiese', {results: results});

});

module.exports = router;

在“ //测试参数:”下,我使用硬编码变量。

我的/ Site在时间轴中链接:

喜欢这个:

    function getItemInfo(id) {
        if (!findObjectByKey(dataSetFürTimeLineErzeugen().concat(getAngepassteMilestones()), "id", id).content.includes("milestone")) {
            console.log("du bist jetzt in getIteminfo() mit dem id = " + id);
            var element = findObjectByKey(rese, "timestamp", id);
            if(element.type.includes("wiki"))window.open("http://localhost:3001/spielwiese/?timestamp="+element.timestamp+"&groupid="+element.group_id+"&userid="+element.user_id+"&type="+element.type);
            else window.open(element.object_id)
        }
    }

最后我的hbs文件是这样的:

<!DOCTYPE html>

<html>

<head>

    <title>{{title}}</title>

    <link rel='stylesheet' href='/stylesheets/wiki.css'/>

</head>

<body>

<script>

    var url_string = window.location.href;

    var url = new URL(url_string);

    var zieltimestamp = url.searchParams.get("timestamp");

    var zieluser_id = url.searchParams.get("userid");

    var zielgroup_id = url.searchParams.get("groupid");

    var zieltype = url.searchParams.get("type");


    console.log(zieltimestamp);
    console.log(zieluser_id);
    console.log(zielgroup_id);
    console.log(zieltype);

</script>

{{#each results}}

    {{{this.compare}}}

{{/each}}



</body>

</html>

现在我不知道如何将这些变量从hbs传递到js脚本中以使用这些变量。控制台日志和分配工作在hbs文件中。

但是我注意到这并不意味着它们在js文件中可用。

猜猜我还需要其他要求吗?

如果我排除了我的测试参数,我会收到错误消息,如果我的csv解析器缺少某些变量,我会得到。

希望您不需要app.js。

我也想请一些简单的参考,我想我还没有收到请求,请发送框架。

谢谢。

干杯吧。

对不起,我的代码太重了,我不知道需要什么。

0 个答案:

没有答案