mongodb 我无法从另一个文件导入代码

8wigbo56  于 2023-02-11  发布在  Go
关注(0)|答案(2)|浏览(134)

我试图将我的代码函数从一个文件导入到另一个文件,但是,它不工作。
查看我从中导出的文件:

const uri = "mongodb+srv://<User>:<PassWord>@cluster0.ubhacr9.mongodb.net/?retryWrites=true&w=majority"


const client = new MongoClient(uri);

async function run(){
    //code
}

async function inserirDatabase(tipo, descricao, valor, id){
   //code
}

async function readDatabase (){
    //code
}

async function deleteOneOnDatabase(id){
   //code
}

module.exports = run, inserirDatabase, readDatabase, deleteOneOnDatabase

看看我是如何导入文件的:import {run, inserirDatabase, readDatabase, deleteOneOnDatabase} from '../database/database.js';

m1m5dgzv

m1m5dgzv1#

尝试在每个函数名前添加“export”,而不是使用module.exports。

export async function run(){
    //code
}
zengzsys

zengzsys2#

导出文件:

function somefunction1(){
 //codes
}

function somefunction2(){
 //codes
}

module.exports = {
 somefunction1: somefunction1,
 somefunction2: somefunction2
}

导入文件:

const {somefunction1, somefunction2} = require("path/to/file.js")
somefunction1()
somefunction2()

相关问题