简体   繁体   English

错误:在我的 NodeJS 应用程序中找不到模块“../../models/blog”

[英]Error: Cannot find module '../../models/blog' in my NodeJS app

this is my code in routes/blog.js这是我在 routes/blog.js 中的代码

var express = require('express');
var router = express.Router();
const Blogs = require("../models/blog");

and this my code in models/blog.js这是我在 models/blog.js 中的代码

const mongoose = require('mongoose');
const mongo = require('mongodb');
const dbUrl = 'mongodb://localhost:27017/BlogDB';

mongoose.connect(dbUrl, { useNewUrlParser: true });

const db = mongoose.connection;
const Schema = mongoose.Schema;

const blogSchema = new Schema({
id: {
    type: Schema.ObjectId
},
title: {
    type: String,
    required: true

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

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

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

},
})

 const Blogs = module.exports = mongoose.model("blogs", blogSchema);
 //module.exports = mongoose.model("blogs", blogSchema);

I've tried some basic solutions.我已经尝试了一些基本的解决方案。 But still can't use npm start, Is there any way I can get it to work?但是仍然无法使用 npm 启动,有什么办法可以让它工作吗? I'm a beginner, please.我是初学者,请。

在我的道路上

In your routes/blog.js edit 3rd line to this const Blogs = require("../views/models/blog");在你的routes/blog.js中,将第 3 行编辑为这个const Blogs = require("../views/models/blog"); should resolve your problem.应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM