javascript/ node.js/ mongodb/ mongoose/ schema

I have created 3 files MongoDBConnect.js, booksSchema.js, Server.js and am running visualstudio.

When I run node server.js I get "Cannot find module '/booksSchema'" error. booksSchema is in the same directory as all other files.

MongoDBConnect.js

mongoose= require('mongoose')

const MONG_URI= 'mongodb://localhost:27017/BooksData'
mongoose.connect(MONG_URI,{useUnifiedTopology:true,useNewUrlParser:true, useFindAndModify:false })
const db= mongoose.connection;
db.on('error',function(err){
console.log('Error occured'+err)
})
db.once('connected',function(){
console.log('connection is successful to'+ MONG_URI)
})

module.exports=db

booksSchema.js

let mongoose= require('mongoose')
const BookScheme= new mongoose.Schema({
booktitle:{
type:String,
required:true
},
PubYear:Number,
author:String,
Topic:String,
formate:String
})

module.exports= mongoose.model('bookmodel',BookScheme,'BookCollection2')

I won't put the whole Server.js code here but here are the first lines

var express = require("express")
let Books = require('/booksSchema')
let mongodbConnected = ('/MongoDBConnect')
const cors = require('cors');

I keep getting throw err;

Error: Cannot find module '/booksSchema'

Why can't it see the booksSchema? They are all in the same directory. Any help would be appreciated

Just so this isn't left unanswered, someone in the comments pointed out that I had missed the dot before my booksSchema path so it should have been './booksSchema' instead of '/booksSchema'.

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question cannot find module 'pouchdb', works on mac, but not on windows Meteor - Uncaught Error: Cannot find module 'mongodb' Cannot find mongoDB module when deploying to Heroku Mac WebStorm Error: Cannot find module 'eslint-config-airbnb' Error: Cannot find module '../build/Release/bson' on Mac Cannot find module 'mongodb/node_modules/bson' MongoDB - Error: Cannot find module '../build/Release/bson' Cannot install npm package (fsevents) on Mac OS Catalina Cannot find module 'reactcomponent' Cannot find module bundle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM