首页 > 解决方案 > 无法读取未定义的属性“关闭”

问题描述

我正在将一个网络应用程序部署到 heroku。但是我遇到了一个本地不会发生的错误。MongoDB 版本:3.6.3 抛出的错误是:

2019-04-04T09:58:24.463888+00:00 app[web.1]: at TLSSocket.emit (events.js:194:15)
2019-04-04T09:58:24.463889+00:00 app[web.1]: at _handle.close (net.js:597:12)
2019-04-04T09:58:24.473758+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-04-04T09:58:24.474134+00:00 app[web.1]: npm ERR! errno 1
2019-04-04T09:58:24.475302+00:00 app[web.1]: npm ERR! Quiniela@1.0.0 start: `node ./src/server.js`
2019-04-04T09:58:24.475457+00:00 app[web.1]: npm ERR! Exit status 1
2019-04-04T09:58:24.475782+00:00 app[web.1]: npm ERR!
2019-04-04T09:58:24.475903+00:00 app[web.1]: npm ERR! Failed at the Quiniela@1.0.0 start script.
2019-04-04T09:58:24.476026+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-04-04T09:58:24.482952+00:00 app[web.1]: 
2019-04-04T09:58:24.483094+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-04-04T09:58:24.483183+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-04-04T09_58_24_477Z-debug.log
2019-04-04T09:58:24.548590+00:00 heroku[web.1]: State changed from starting to crashed
2019-04-04T09:58:24.531532+00:00 heroku[web.1]: Process exited with status 1
2019-04-04T11:35:29.837751+00:00 heroku[web.1]: State changed from crashed to starting
2019-04-04T11:35:48.925363+00:00 heroku[web.1]: Starting process with command `npm start`
2019-04-04T11:35:50.613487+00:00 app[web.1]: 
2019-04-04T11:35:50.613511+00:00 app[web.1]: > Quiniela@1.0.0 start /app
2019-04-04T11:35:50.613513+00:00 app[web.1]: > node ./src/server.js
2019-04-04T11:35:50.613514+00:00 app[web.1]: 
2019-04-04T11:35:51.950629+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2019-04-04T11:35:51.950664+00:00 app[web.1]: designed for a production environment, as it will leak
2019-04-04T11:35:51.950666+00:00 app[web.1]: memory, and will not scale past a single process.
2019-04-04T11:35:51.981763+00:00 app[web.1]: Server start in http://localhost: 30131
2019-04-04T11:35:52.150746+00:00 app[web.1]: /app/node_modules/mongodb/lib/topologies/replset.js:368
2019-04-04T11:35:52.150749+00:00 app[web.1]: throw err;
2019-04-04T11:35:52.150750+00:00 app[web.1]: ^
2019-04-04T11:35:52.150752+00:00 app[web.1]: 
2019-04-04T11:35:52.150753+00:00 app[web.1]: TypeError: Cannot read property 'close' of undefined
2019-04-04T11:35:52.150755+00:00 app[web.1]: at topology.connect (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:412:16)
2019-04-04T11:35:52.150759+00:00 app[web.1]: at ReplSet.<anonymous> (/app/node_modules/mongodb/lib/topologies/replset.js:364:11)
2019-04-04T11:35:52.150760+00:00 app[web.1]: at Object.onceWrapper (events.js:277:13)
2019-04-04T11:35:52.150762+00:00 app[web.1]: at ReplSet.emit (events.js:189:13)
2019-04-04T11:35:52.150767+00:00 app[web.1]: at /app/node_modules/mongodb-core/lib/topologies/replset.js:631:23
2019-04-04T11:35:52.150769+00:00 app[web.1]: at Server.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/replset.js:357:9)
2019-04-04T11:35:52.150770+00:00 app[web.1]: at Object.onceWrapper (events.js:277:13)
2019-04-04T11:35:52.150771+00:00 app[web.1]: at Server.emit (events.js:189:13)
2019-04-04T11:35:52.150772+00:00 app[web.1]: at Object.cb (/app/node_modules/mongodb-core/lib/topologies/server.js:422:25)
2019-04-04T11:35:52.150774+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:289:33)
2019-04-04T11:35:52.150775+00:00 app[web.1]: at Object.onceWrapper (events.js:277:13)

我正在使用 Node.js + Express 和 monogoose,连接到 MongoDB Atlas。

我不确定错误的原因。我已经查看了路线、猫鼬模型以及与 BD 的连接。但我没有找到问题的根源

编码。数据库连接:

const mongoose = require('mongoose');
const { mongodb } = require('./keys');
const options = {
  useNewUrlParser: true,
  useCreateIndex: true,
  useFindAndModify: false,
  autoIndex: false, 
  reconnectTries: Number.MAX_VALUE, 
  reconnectInterval: 500, 
  poolSize: 10, 
  bufferMaxEntries: 0,
  connectTimeoutMS: 10000, 
  socketTimeoutMS: 45000, 
  family: 4 
};

// mongodb.URI: mongodb+srv://xxxx:xxxx@xxxx-rbrmd.mongodb.net/test?retryWrites=true
connectTo=()=>{
  mongoose.connect(mongodb.URI, options)
    .then(db => console.log('DB is connected'))
    .catch(err => console.log("Error: " + JSON.stringify(err)));

}

connectTo()

module.exports = connectTo;

模型:

const mongoose = require('mongoose');

const { Schema } = mongoose;

const listEquipos = new Schema({
  nombreEquipo: String,
  thumbnail: String
});
const listaDeEquipos =mongoose.model('equipos_ligamx', listEquipos);

const partidos_pendientes_LigaMx = new Schema({
  equipoLocal: String,
  thumbnailLocal: String,
  equipoVisitante: String,
  thumbnailVisitante: String,
  fecha: String,
  hora: String
});
const partidosPendientesLigaMx = mongoose.model('partidos_pendientes_ligamx', partidos_pendientes_LigaMx);

const partidos_jugados_LigaMx = new Schema({
  equipoLocal: String,
  thumbnailLocal: String,
  equipoVisitante: String,
  thumbnailVisitante: String,
  year: String,
  resultado: String,
  ganador: String
});
const partidosJugadosLigaMx = mongoose.model('partidos_jugados_ligamx', partidos_jugados_LigaMx);

module.exports = {
  listaDeEquipos,
  partidosPendientesLigaMx,
  partidosJugadosLigaMx
}

还有我保存在 BD 中的另一个文件:

const puppeteer = require('puppeteer');
const mongoose = require('mongoose');
const { listEquiposLigaMx, partidosPendientesLigaMx, partidosJugadosLigaMx } = require('../../models/liga-mx')
var fs = require("fs");
var url = 'https://www.marca.com/claro-mx/futbol/liga-mx/clausura/calendario.html'

getPartidosPendientesLigaMx=()=>{

puppeteer.launch()
.then(async (browser) => {
    const page = await browser.newPage();
    await page.goto(url);
    let equipo = await page.evaluate(()=>{

        const img_local = [
            ...document.querySelectorAll('.local figure img')
        ]

        const img_visitante = [
            ...document.querySelectorAll('.visitante figure img')
        ]

        const equipo_local = [
            ...document.querySelectorAll('.local span')
        ].map((equipoLocal)=> equipoLocal.innerText);

        const equipo_visitante = [
            ...document.querySelectorAll('.visitante span')
        ].map((equipoVisitante)=> equipoVisitante.innerText);

        const fecha = [
            ...document.querySelectorAll('.resultado .fecha')
        ].map((fecha)=> fecha.innerText);

        const hora = [
            ...document.querySelectorAll('.resultado .hora')
        ].map((hora)=> hora.innerText);
        const size = equipo_local.length;
        var resize = equipo_local.length-fecha.length-1;
        var indicador = -1;
        var date = new Date()
        var day = date.getDate()
        var month = date.getMonth()


        return equipo_local.map((item, index)=>{
            if(resize>index-1 && resize<img_local.length-1){
                resize++
                indicador++
                return(
                    ({
                        equipoLocal: img_local[resize].alt,
                        thumbnailLocal: img_local[resize].src,
                        equipoVisitante: img_visitante[resize].alt,
                        thumbnailVisitante: img_visitante[resize].src,
                        year: 2019,
                        fecha: fecha[indicador],
                        hora: hora[indicador]

                    })
                )
            }
        })
    })

    var equipoParse = equipo.filter(function (el) {
        return el != null;
      });

    partidosPendientesLigaMx.insertMany(equipoParse, function(err) {
        if (err) {console.log("El error papu: ",err)}
        console.log("Saved!")

    });
    await browser.close();
  }).
  catch((err)=> console.log('> Error: ', err))
}

标签: node.jsmongodbexpress

解决方案


推荐阅读