在电子应用程序中使用节点请求时获取“ self._form.on不是函数”

时间:2018-09-17 22:26:52

标签: javascript node.js vue.js webpack electron

我正在使用Vue.js作为客户端框架来构建Electron应用程序。当我尝试运行节点脚本时,出现未捕获(承诺)TypeError:self._form.on不是函数

我不确定这是否是由于它是通过电子在浏览器上运行的,但是当我通过控制台中的Node运行脚本时,它运行良好。甚至可以运行此节点脚本客户端吗?我通过要求文件通过前端vue.js组件之一运行节点文件。 让raffleScript = require('../ raffles / index.js');

节点脚本:

var cheerio = require('cheerio');
var fs = require('fs');

var retryDelay = 300; 
var tasks = 200; 


var userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36';
var tasksComplete = 0;
var exportedCount = 0;
// Export the values
//setInterval(countUpdate, 4000);

initProxies();

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

async function initProxies() {
    for (var i = 0; i < tasks; i++) {
        submitRaffle(i);
        await sleep(retryDelay);
        // setTimeout(function () {
        //  submitRaffle(i);
        // }, retryDelay)
    }
}

function submitRaffle(taskNum) {
    var jar = require('request').jar()
    var request = require('request').defaults({
        jar: jar
    });


    console.log('Making request.............................');
    request({
        url: 'bungeedesign.com/dev',
        method: 'POST',
        headers: {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        formData: 
        { firstname: 'James',
          name: '',
          lastname: 'Rogers',
          mobileNumber: '00000000',
          email: 'email@email.com',
          raffle_type: 'Development',
          checkbox_0: 'Backend',
          personal_or_business: 'Personal',
          model: 'Register Of Interests',
          'submit-0': 'Keep me posted' },
    }, function (error, response, body) {
        // console.log('REQ RES --------->>>> ', response);
        //console.log('Req Completed', response);
        if (error) {
            console.log('Error.............................');
            setTimeout(function () {
                submitRaffle(taskNum)
            }, retryDelay)
            return;
        }
        $ = cheerio.load(body);
        if (response.statusCode == 302) {
            //if (JSON.parse(body).message == "successfully") {
                console.log('\x1b[1;32m', "[" + taskNum + "] - Raffle entry submitted");
                tasksComplete++;
                //module.exports.exportedCount = tasksComplete;
                console.log('\x1b[1;32m', "[" + taskNum + "] - " + tasksComplete + " Entries submitted.")
            //}
        } else {
            setTimeout(function () {
                submitRaffle(taskNum)
            }, retryDelay)
            return;
        }
    });
}


// Increment Exports

// function countUpdate() {
//  module.exports.exportedCount = exportedCount++;
// }

Main.vue组件(脚本)

let raffleScript = require('../raffles/index.js');
    export default {
        data() {
            return {
                amount: '0'
            }
        },
        methods: {
            async register() {
                try {
                    await AuthenticationService.register({
                        key: this.key
                    })
                } catch (error) {
                    this.error = error.response.data.error
                }
            },

            async entryAmountChange() {
                let entryAmount = document.querySelector('.tsEntryAmount').value;
                let replaced = entryAmount.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                document.querySelector('.tsEntryAmount').value = replaced;               
            },
            async runRaffle() {
                console.log("Log: ", raffleScript.exportedCount);
                this.entryAmount = raffleScript.exportedCount;
            },
            runTime: function() {
                function checkTime(i) {
                    return (i < 10) ? "0" + i : i;
                }

                function startTime() {
                    let today = new Date(),
                        h = checkTime(today.getHours()),
                        m = checkTime(today.getMinutes()),
                        s = checkTime(today.getSeconds());
                    let timeElement = document.querySelector('#time');
                    let time = h + ":" + m + ":" + s;
                    // this.time = "dsdsd";
                    document.querySelector('#time').innerHTML = time;
                    setTimeout(function () {
                        startTime()
                    }, 500);
                }
                startTime();
            },
            async getValues() {
                console.log("Log: ", raffleScript.tasksComplete);
            }
        },
        mounted(){
            this.runTime();
            this.getValues();
            this.runRaffle();
        }
    }

1 个答案:

答案 0 :(得分:0)

无法使其与“节点请求”模块一起使用。我尝试了Axios,尽管我在CORS方面遇到了一些问题,但是仍然可以使用。然后,我遇到了工作得很好的Needle https://www.npmjs.com/package/needle

不确定使用formData时节点请求为何不起作用。