【问题标题】:Nodemon error with mondoDB: " app crashed - waiting for file changes before starting"mongoDB 的 Nodemon 错误:“应用程序崩溃 - 启动前等待文件更改”
【发布时间】:2020-08-25 13:12:22
【问题描述】:

我知道为什么会发生这种情况,我已经阅读了 5 次代码,但我看不到错误。 (我用过requireDir)

server.js 代码:

//chamando 
const express = require('express');
const mongoose = require("mongoose");
const requireDir = require('require-dir');
//iniciando app
const app = express();

//iniciando o DB
mongoose.connect(
    "mongodb://localhost:27017/nodeapi",
    { useNewUrlParser: true }
);

requireDir('.src/models');

//first rote
app.get('/', (req, res) => {

    res.send('hello worlldd');
});

app.listen(3001); //porta

//NODEMON - LIVE 
/* "dev": "nodemon server.js" no package
npm run dev no terminal */

Product.js 代码:

const mongoose = require('mongoose');

const ProductSchema = new mongoose.Schema({
    title:{
        type: String,
        required: true,
    },

    description:{
        type: String,
        required: true,
    },

    url:{
        type: String,
        required: true,
    },

    createdAt:{
        type: Date,
        default: Date.now,
    },
});

mongoose.model('product', ProductSchema);

服务器只是不运行。在我放“猫鼬”之前一切正常。

我也使用过 docker 和 robots3T。

整个错误

【问题讨论】:

  • 是的,您是否为您的 mongodb 数据库使用任何身份验证?并且请发布整个错误。不仅仅是app crashed

标签: javascript node.js mongodb nodemon


【解决方案1】:

requireDir() 提供的路径路径有错字。您包含路径 .src/models 而不是 ./src/models

固定server.js:

//chamando 
const express = require('express');
const mongoose = require("mongoose");
const requireDir = require('require-dir');
//iniciando app
const app = express();

//iniciando o DB
mongoose.connect(
    "mongodb://localhost:27017/nodeapi",
    { useNewUrlParser: true, useUnifiedTopology: true }
);

requireDir('./src/models');

//first rote
app.get('/', (req, res) => {

    res.send('hello worlldd');
});

app.listen(3001); //porta

//NODEMON - LIVE 
/* "dev": "nodemon server.js" no package
npm run dev no terminal */

【讨论】:

  • 谢谢伙计。正是这样,多么愚蠢的错误。我现在开始学习mongo和node,我犯了一个愚蠢的错误。
  • 对于有同样错误的人来说,是它出现在终端中: ` [nodemon] 由于更改而重新启动... [nodemon] 开始node server.js (node :2140) 弃用警告:当前的服务器发现和监控引擎已被弃用,并将在未来的版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。 (使用node --trace-deprecation ... 显示警告的创建位置)` 并且“localhost:3001”正常打开
  • 要修复此警告消息,请将{ useNewUrlParser: true, useUnifiedTopology: true } 作为mongoose.connect 的配置选项。
  • 谢谢,在这里见到巴西朋友总是很高兴。不幸的是,在巴西论坛上没有人回答。
  • 你知道Rocketseat community吗?他们的 Discord 频道中有很多开发人员互相帮助,主要语言是葡萄牙语。此外,他们分享了很多关于 Javascript 的有用内容。稍后看看,我强烈推荐你。
猜你喜欢
  • 1970-01-01
  • 2021-07-02
  • 2022-11-11
  • 2020-01-19
  • 2023-02-12
  • 2022-01-15
  • 2018-04-29
  • 2017-10-15
  • 2016-09-25
相关资源
最近更新 更多