Node.js-如何在函数内调用方法?

时间:2019-09-10 14:49:58

标签: javascript node.js sequelize.js

我正在尝试在NodeJS上创建一个执行集成的服务-目前,我所拥有的只是一个初始化函数和另一个名为execute的

但是,我无法在“初始化”函数中调用“执行”函数,该怎么办?

预先感谢


我已经尝试过:

  • 使用this.execute();
  • 使用SchedulerService.execute();
  • 使用execute();
  • 使用var _this = this;_this.execute();
  • 将execute用作scheduleJon(..., function(execute){内的参数

import schedulerConf from '../../config/scheduler';
import request from 'request';
import schedule from 'node-schedule';
import dateformat from 'dateformat';

let interations = 0;

const SchedulerService = function(){

    return {

        initialize: async () => {
                console.log(`***** Starting Scheduler on ${dateformat(new Date(), "dd-mm-YYYY h:MM:ss")}`);
                var j = schedule.scheduleJob('*/1 * * * *', function(){
                console.time('Scheduler execution time');
                if(interations === 0){
                    console.log(`Setting scheduler runtime to full time.`);
                }
                interations++;
                SchedulerService.execute();
                console.log(`Job executed ${interations} times.`);
                console.timeEnd('Scheduler execution time');
            });

        },

        execute: async () => {
            var options = {
                url: 'http://example.com',
                method: 'GET'
            }      
            return new Promise(function (resolve, reject) {
                request(options, function (error, res, body) {
                    if (!error && res.statusCode == 200) {
                        console.log
                        resolve(JSON.parse(body));
                    } else {
                        reject(error);
                    }
                });
            });    
        }
    }
}
export default new SchedulerService();

3 个答案:

答案 0 :(得分:0)

您不能调用<v-footer app inset fixed > <v-row align="center"> <v-col class="shrink px-1"> <v-tooltip top> <template v-slot:activator="{ on }"> <v-btn icon outlined v-on="on" @click="dryRun" > <v-icon>flash_on</v-icon> </v-btn> </template> <span>Tooltip</span> </v-tooltip> </v-col> </v-row> </v-footer> ,因为使用箭头功能时,this.execute()关键字是指封闭的函数上下文,而不是对象上下文(即您要查找的内容)。

为此,您必须使用简单的函数。

this

答案 1 :(得分:0)

我认为,如果在execute语句之外定义return函数,就可以解决问题。

import schedulerConf from '../../config/scheduler';
import request from 'request';
import schedule from 'node-schedule';
import dateformat from 'dateformat';

let interations = 0;

const SchedulerService = function(){

    const execute = async () => {
            var options = {
                url: 'http://example.com',
                method: 'GET'
            }      
            return new Promise(function (resolve, reject) {
                request(options, function (error, res, body) {
                    if (!error && res.statusCode == 200) {
                        console.log
                        resolve(JSON.parse(body));
                    } else {
                        reject(error);
                    }
                });
            });    
        }

    return {

        initialize: async () => {
                console.log(`***** Starting Scheduler on ${dateformat(new Date(), "dd-mm-YYYY h:MM:ss")}`);
                var j = schedule.scheduleJob('*/1 * * * *', function(){
                console.time('Scheduler execution time');
                if(interations === 0){
                    console.log(`Setting scheduler runtime to full time.`);
                }
                interations++;
                execute();
                console.log(`Job executed ${interations} times.`);
                console.timeEnd('Scheduler execution time');
            });

        },

        execute
    }
}
export default new SchedulerService();

答案 2 :(得分:0)

您的函数返回其他函数,这意味着该函数应在您访问内部之前运行。 这意味着您可以这样称呼它。

SchedulerService().execute();
相关问题