[英]The requested module './server/controller/controller.js' does not provide an export named 'default'
I am currently new at backend and using ES6 modules
in my project我目前是后端新手并在我的项目中使用
ES6 modules
when I import controller file to index.js
and start the project it says :-当我将控制器文件导入
index.js
并启动项目时,它说:-
Syntax Error: The requested module './server/controller/controller.js' does not provide an export named 'default'语法错误:请求的模块“./server/controller/controller.js”不提供名为“default”的导出
and when I import it as :-当我将其导入为:-
import {controller} from './server/controller/controller.js'
^^^^^^^^^^
Syntax Error: The requested module './server/controller/controller.js' does not provide an export named 'controller'
so what i am doing wrong here i also mention "type": "module" in my package.所以我在这里做错了什么我还在我的包中提到了“类型”:“模块”。 json file
json文件
index.js file index.js 文件
import express from 'express'
import dotenv from 'dotenv'
import morgan from 'morgan'
import path from 'path';
import mongoose from "mongoose";
import {controller} from './server/controller/controller.js'
import route from './server/routes/route.js';
const __dirname = path.resolve();
const app = express();
//mongoDb
const connectDB = async () => {
try{
// mongodb connection string
const con = await mongoose.connect(process.env.MONGO, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
console.log(`MongoDB connected `);
}catch(err){
console.log(err);
}
}
//mongodb finish
dotenv.config({path:'confige.env'})
const PORT = process.env.PORT || 5000
app.use(morgan('tiny'));
//database
connectDB()
//view engine
app.use(express.urlencoded({ extended : true}))
app.set("view engine", "ejs")
app.use('/css', express.static(path.resolve(__dirname, "assets/css")))
app.use('/js', express.static(path.resolve(__dirname, "assets/js")))
//routes
app.use('/',route)
// routes finish
//controller
app.post('/api/users',controller.create)
app.put('/api/users',controller.update)
app.delete('/api/users',controller.delete)
app.get('/api/users',controller.find)
app.listen(PORT, ()=> { console.log(`Server is running on http://localhost:${PORT}`)});
controller.js file控制器.js文件
import Userdb from "../model/model.js";
exports.create = (req,res) =>{
}
exports.find = (req,res) =>{
}
exports.update = (req,res) =>{
}
exports.delete = (req,res) =>{
}
model.js file模型.js文件
import mongoose from "mongoose";
const scheme = new mongoose.Schema({
name:{
type:String,
required:true
},
email:{
type:String,
required:true,
unique:true
},
gendr:String,
status:String
})
const Userdb = mongoose.model('userdb',scheme)
export default Userdb
should i need to replace exports in controller.js with Userdb beacause i am using ES6 module ?我是否需要用 Userdb 替换 controller.js 中的导出,因为我使用的是 ES6 模块? and if so than what will be the code of controller.js file
如果是这样,那么 controller.js 文件的代码是什么
thanks in advance :)提前致谢 :)
There are two types of exports in ES6: name exports(one or more) and default(one per module). ES6 中有两种类型的导出:名称导出(一个或多个)和默认导出(每个模块一个)。
Named exports are helpful when you have more than one thing to export from the modules and in your case I think you use use it.当您从模块中导出不止一件东西时,命名导出很有用,在您的情况下,我认为您使用它。
In your server.js file, you are using destructing in the import, however, you are not exporting in the form of object which has controller
key in it.在您的 server.js 文件中,您在导入中使用了析构,但是,您不是以其中包含
controller
键的对象形式导出。
In your controller.js:在你的 controller.js 中:
import Userdb from "../model/model.js";
const create = (req,res) =>{
}
const find = (req,res) =>{
}
const update = (req,res) =>{
}
const _delete = (req,res) =>{ }
export {create, find, update, _delete}
In your index.js :在你的 index.js 中:
import { create, find, update, _delete } from 'controller.js';
I suggest you to check this link, how import and export works in ES6.我建议你检查这个链接,ES6 中的导入和导出是如何工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.