Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Heroku Nodejs Reddit机器人在本地运行良好,但不能在线运行_Javascript_Node.js_Heroku - Fatal编程技术网

Javascript Heroku Nodejs Reddit机器人在本地运行良好,但不能在线运行

Javascript Heroku Nodejs Reddit机器人在本地运行良好,但不能在线运行,javascript,node.js,heroku,Javascript,Node.js,Heroku,我有一个用Nodejs编写的带有Snoowrap和Snoostorm的reddit机器人,并部署到Heroku。 我的日志不断地抛出这个错误: 2020-03-13T06:02:53.784219+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: RequestError: Error: ESOCKETTIMEDOUT 2020-03-13T06:02:53.784229+00:00 app[web.1]: at n

我有一个用Nodejs编写的带有Snoowrap和Snoostorm的reddit机器人,并部署到Heroku。 我的日志不断地抛出这个错误:

2020-03-13T06:02:53.784219+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: RequestError: Error: ESOCKETTIMEDOUT
2020-03-13T06:02:53.784229+00:00 app[web.1]:     at new RequestError (/app/node_modules/request-promise-core/lib/errors.js:14:15)
2020-03-13T06:02:53.784230+00:00 app[web.1]:     at Request.plumbing.callback (/app/node_modules/request-promise-core/lib/plumbing.js:87:29)
2020-03-13T06:02:53.784231+00:00 app[web.1]:     at Request.RP$callback [as _callback] (/app/node_modules/request-promise-core/lib/plumbing.js:46:31)
2020-03-13T06:02:53.784231+00:00 app[web.1]:     at self.callback (/app/node_modules/request/request.js:185:22)
2020-03-13T06:02:53.784234+00:00 app[web.1]:     at Request.emit (events.js:311:20)
2020-03-13T06:02:53.784234+00:00 app[web.1]:     at ClientRequest.<anonymous> (/app/node_modules/request/request.js:819:16)
2020-03-13T06:02:53.784235+00:00 app[web.1]:     at Object.onceWrapper (events.js:417:28)
2020-03-13T06:02:53.784235+00:00 app[web.1]:     at ClientRequest.emit (events.js:311:20)
2020-03-13T06:02:53.784236+00:00 app[web.1]:     at TLSSocket.emitRequestTimeout (_http_client.js:714:9)
2020-03-13T06:02:53.784237+00:00 app[web.1]:     at Object.onceWrapper (events.js:417:28)
2020-03-13T06:02:53.784237+00:00 app[web.1]:     at TLSSocket.emit (events.js:311:20)
2020-03-13T06:02:53.784237+00:00 app[web.1]:     at TLSSocket.Socket._onTimeout (net.js:478:8)
2020-03-13T06:02:53.784238+00:00 app[web.1]:     at listOnTimeout (internal/timers.js:549:17)
2020-03-13T06:02:53.784238+00:00 app[web.1]:     at processTimers (internal/timers.js:492:7)
2020-03-13T06:02:53.784281+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)`
奇怪的是日志没有告诉我代码中的错误来源,所以我有点不知所措。 我已经在我的机器上运行了几个小时,但是当部署到Heroku时,它会在大约1分钟后抛出这个错误

请帮助:(

编辑:这是错误对象,它对我没有帮助,但可能会帮助您解决此问题:(

{
名称:“RequestError”,
消息:“错误:esocketimedout”,
原因:错误:esocketimedout
在ClientRequest上。(/app/node\u modules/request/request.js:816:19)
在Object.onceWrapper(events.js:417:28)
在ClientRequest.emit(events.js:311:20)
在TLSSocket.emitRequestTimeout(_http_client.js:714:9)
在Object.onceWrapper(events.js:417:28)
在TLSSocket.emit(events.js:311:20)
在TLSSocket.Socket.\u onTimeout(net.js:478:8)
在listOnTimeout(internal/timers.js:549:17)
在processTimers(internal/timers.js:492:7){
代码:'eSocketimedout',
连接:false
},
错误:错误:esocketimedout
在ClientRequest上。(/app/node\u modules/request/request.js:816:19)
在Object.onceWrapper(events.js:417:28)
在ClientRequest.emit(events.js:311:20)
在TLSSocket.emitRequestTimeout(_http_client.js:714:9)
在Object.onceWrapper(events.js:417:28)
在TLSSocket.emit(events.js:311:20)
在TLSSocket.Socket.\u onTimeout(net.js:478:8)
在listOnTimeout(internal/timers.js:549:17)
在processTimers(internal/timers.js:492:7){
代码:'eSocketimedout',
连接:false
},
选项:{
gzip:是的,
是的,
标题:{'user agent':'myuseragent'},
baseUrl:'https://oauth.reddit.com',
qs:{raw_json:1},
auth:{持有者:'myrefreshtoken'},
resolveWithFullResponse:true,
超时:5000,
transform:[函数:transform],
uri:'comments/fhxl4r',
方法:“GET”,
回调:[函数:RP$callback],
简单:没错,
莫利:错
},
答复:未定义
}

首先确保您的开发和生产环境尽可能相似。软件包安装正确吗?您是否在同一个节点版本上?@Tin Nguyen我已将两个节点版本都设置为12.x,并在package.json中列出了所需的所有依赖项。
require('dotenv').config();
const MyUtil = require("./myutil.js")
const { CommentStream, SubmissionStream, ModMailStream, InboxStream } = require("snoostorm");
const Snoowrap = require('snoowrap');
const Snoostorm = require('snoostorm');
const WatchJS = require("melanke-watchjs")
const r = new Snoowrap({
    userAgent: 'abcde',
    clientId: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET,
    refreshToken: process.env.REFRESH_TOKEN
});
const BOT_START = Date.now() / 1000;
var webController;

function initializeBot(controller){
    configReddit();
    watchRateLimit();
    initCommentStream();
    initPostStream();
    initInboxStream();
    initModMailStream();
    webController = controller;
    return module.exports
}

function configReddit(){
    r.config({continueAfterRatelimitError: true});
    console.info("Finished Reddit configuration.")
}

function watchRateLimit(){
    //WATCH JS
// var watch = WatchJS.watch;
// var unwatch = WatchJS.unwatch;
// var callWatchers = WatchJS.callWatchers;

    WatchJS.watch(r, "ratelimitRemaining", 
    () => {
        if(r.ratelimitRemaining < 50){
            console.warn("Rate limit remaining:" +r.ratelimitRemaining);
        }
        if(webController){
            webController.broadcast(r.ratelimitRemaining);
        }
    });
}

function initCommentStream(){
    console.info("Trying to establish comment stream!");
    var streamOpts;
    try {
        streamOpts = JSON.parse(process.env.COMMENT_STREAM_OPTION);
        if(!streamOpts || !streamOpts.receiving){
            console.info("Comment Stream was disabled, enable through the environment variable.")
            return;
        }
    } catch (error) {
        console.log(error);
        console.info("COMMENT_STREAM_OPTION unavailable/wrong format.");
        return;
    }

    const comments = new CommentStream(r, streamOpts);
    comments.on("item", comment => {
        if(comment.created_utc < BOT_START) return;
        if(comment.body.toLowerCase().includes("!resolved")){
            console.log("New resolved comment!: "+comment.link_id.substring(3))
            let flair = {flair_template_id:MyUtil.FLAIR_ID.RESOLVED}
            let sub = r.getSubmission(comment.link_id.toString().substring(3));
            let reply = ""; //TODO
            sub.selectFlair(flair).then(sub.reply())
        }
    })

    console.info("Comment Stream established.");
}

function initPostStream(){
    console.info("Trying to establish post stream!");
    var streamOpts
    try {
        streamOpts = JSON.parse(process.env.POST_STREAM_OPTION);
        if(!streamOpts || !streamOpts.receiving){
            console.info("Post Stream was disabled, enable through the environment variable.")
            return;
        }
    } catch (error) {
        console.log(error)
        console.info("POST_STREAM_OPTION unavailable/wrong format.");
        return;
    }
    const posts = new Snoostorm.SubmissionStream(r, streamOpts);
    //*Listen for items (posts)
    posts.on("item", post =>{
        if(post.created_utc < BOT_START) return;
        console.log("New POST");
        console.log(post.body);
        notifyNewPost("/u/abcde", post)
        //TODO
    })
    console.info("Post Stream established!");
}

function initInboxStream(){
    //TODO
}

function initModMailStream(){
    //TODO
}

async function notifyNewPost(peopleList, post){
    if(!post._hasFetched){
        try {
            post = await post.fetch();
        } catch (error) {
            console.log("Error at notifyNewPost")
            console.log(error);
            return
        }
    }
    var sendFunction = function(people, post){
        r.composeMessage({
            to:people,
            subject:"New post in r/GoogleAppsScript",
            text:`There's a new post in r/GoogleAppsScript:
            [${post.title}](${post.url}) posted by ${post.author.name}
            `
        }).then(()=>{console.log(`New post notification sent to ${people}.`)})
        .catch((err)=>{console.log("Error on sending noti to abcde:\n"+err)});
    }
    if(typeof peopleList == "string"){
        sendFunction(peopleList, post);
    }else{
        for (const people of peopleList) {
            sendFunction(people, post);
        }
    }
}
module.exports={
    initializeBot,
    r
}
const controller = {};
var express = require('express');
var app = express();
const wakeDyno = require("woke-dyno");
const bot = require("./app").initializeBot(controller);

var http = require('http').createServer(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
   res.sendFile(__dirname + '/html/index.html');
});

io.on('connection', function(socket){
   console.log('a user connected');
});

http.listen(port, ()=>{
   wakeDyno("https://gas-lighter-bot.herokuapp.com/").start(); 
   console.log('listening on *:'+port);
});

controller.broadcast = (msg) => {
   io.emit("ratelimitChanged",msg);
};
{
  name: 'RequestError',
  message: 'Error: ESOCKETTIMEDOUT',
  cause: Error: ESOCKETTIMEDOUT
      at ClientRequest.<anonymous> (/app/node_modules/request/request.js:816:19)
      at Object.onceWrapper (events.js:417:28)
      at ClientRequest.emit (events.js:311:20)
      at TLSSocket.emitRequestTimeout (_http_client.js:714:9)
      at Object.onceWrapper (events.js:417:28)
      at TLSSocket.emit (events.js:311:20)
      at TLSSocket.Socket._onTimeout (net.js:478:8)
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7) {
    code: 'ESOCKETTIMEDOUT',
    connect: false
  },
  error: Error: ESOCKETTIMEDOUT
      at ClientRequest.<anonymous> (/app/node_modules/request/request.js:816:19)
      at Object.onceWrapper (events.js:417:28)
      at ClientRequest.emit (events.js:311:20)
      at TLSSocket.emitRequestTimeout (_http_client.js:714:9)
      at Object.onceWrapper (events.js:417:28)
      at TLSSocket.emit (events.js:311:20)
      at TLSSocket.Socket._onTimeout (net.js:478:8)
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7) {
    code: 'ESOCKETTIMEDOUT',
    connect: false
  },
  options: {
    gzip: true,
    json: true,
    headers: { 'user-agent': 'myuseragent' },
    baseUrl: 'https://oauth.reddit.com',
    qs: { raw_json: 1 },
    auth: { bearer: 'myrefreshtoken' },
    resolveWithFullResponse: true,
    timeout: 5000,
    transform: [Function: transform],
    uri: 'comments/fhxl4r',
    method: 'GET',
    callback: [Function: RP$callback],
    simple: true,
    transform2xxOnly: false
  },
  response: undefined
}