NodeJS无法读取未定义的属性“长度”-MagicMirror模块

时间:2019-10-16 01:41:24

标签: javascript node.js request electron magic-mirror

我正在研究MagicMirror模块的原型,该模块可从json提要https://jsonplaceholder.typicode.com/todos/1中提取数据。它可以在json解析有效负载上正常运行,但是在tArray长度内的某个地方失败,无法准备好未定义的属性“ length”。我怀疑JSON无法正确地传递到tArray,但是我很茫然。以下是我的两个文件!

node_helper.js

'use strict';

/* Magic Mirror
 * Module:
 */

const NodeHelper = require('node_helper');
var request = require('request');
var moment = require('moment');

module.exports = NodeHelper.create({

    start: function() {
        this.started = false;
        this.config = null;
    },

    getData: function() {
        var self = this;
        var myUrl = "https://jsonplaceholder.typicode.com/todos/1";
        request({
            url: myUrl,
            method: 'GET',
            headers: { 'API_TOKEN': this.config.apiKey }
        }, function (error, response, body) {
            console.log(body);
            if (!error && response.statusCode == 200) {
                self.sendSocketNotification("DATA", body);
            }
        });

        setTimeout(function() { self.getData(); }, this.config.refreshInterval);
    },

    socketNotificationReceived: function(notification, payload) {
        var self = this;
        if (notification === 'CONFIG' && self.started == false) {
            self.config = payload;
            self.sendSocketNotification("STARTED", true);
            self.getData();
            self.started = true;
        }
    }
});

MMM-Fi.js


/* Magic Mirror

 */

Module.register('MMM-Fi',{

    defaults: {
        animationSpeed: 1000,
        refreshInterval: 1000 * 60, //refresh every minute
        updateInterval: 1000 * 3600, //update every hour
        lang: config.language,
        initialLoadDelay: 0, // 0 seconds delay
        retryDelay: 5,
    },

    // Define required scripts.
    getScripts: function() {
        return ["moment.js", "font-awesome.css"];
    },

    getStyles: function() {
        return ['MMM-Fi.css'];
    },

    start: function() {
        Log.info('Starting module: ' + this.name);
        this.loaded = false;
        this.sendSocketNotification('CONFIG', this.config);
    },

    getDom: function() {
        var wrapper = document.createElement("div");

        if (this.config.apiKey === "") {
            wrapper.innerHTML = "No Fi <i>apiKey</i> set in config file.";
            wrapper.className = "dimmed light small";
            return wrapper;
        }

        if (this.config.googleApiKey === "") {
            wrapper.innerHTML = "No Google <i>api Key</i> set in config file.";
            wrapper.className = "dimmed light small";
            return wrapper;
        }

        if (!this.loaded) {
            wrapper.innerHTML = this.translate('LOADING');
            wrapper.className = "dimmed light small";
            return wrapper;
        }

        if (!this.tArray.length) {
            wrapper.innerHTML = "No data";
            wrapper.className = "dimmed light small";
            return wrapper;
        }

        var table = document.createElement("table");
        table.id = "fitable";
        table.className = "small thin light";

        var row = document.createElement("tr");

        var lineHeader = document.createElement("th");
        lineHeader.innerHTML = "test2";
        lineHeader.colSpan = 2;
        row.appendChild(lineHeader);
        table.appendChild(row);

        for (var i in this.tArray) {
            var currentDeparture = this.tArray[i];
            console.log(currentDeparture.title)
        }
        wrapper.appendChild(table);
        return wrapper;
    },

    tFi: function(data) {
        if (!data.listT) {
            return;
        }

    this.tArray = [];
    for (var i in data.listT) {
        var t = data.listT[i];
        this.tArray.push({
            userId: t.userId,
            id: t.id,
            title: t.title,
            completed: t.completed,
        });

    }

    return;
},
    socketNotificationReceived: function(notification, payload) {
            if (notification === "STARTED") {
                this.updateDom();
            }
            else if (notification === "DATA") {
                this.loaded = true;
                this.tFi(JSON.parse(payload));
                this.updateDom();
            }
    }

});

0 个答案:

没有答案