我在控制臺中遇到錯誤,請您幫助我嘗試運行我的快速服務器。我的目標是能夠運行 server.js。
import app from './server';
import mongodb, { MongoClient } from 'mongodb';
import dotenv from 'dotenv';
dotenv.config();
const MongoClient = mongodb.MongoClient;
const port = process.env.PORT || 8000;
MongoClient.connect(process.env.RESTREVIEWS_DB_URI, {
maxPoolSize: 50,
wtimeoutMS: 2500,
useNewUrlParser: true,
})
.catch((err) => {
console.error(err.stack);
process.exit(1);
})
.then(async (client) => {
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
});
然后在我運行 server.js 后顯示控制臺
file:///C:/Users/jayde/Desktop/react/restaurant-reviews/backend/index.js:5 const MongoClient = mongodb.MongoClient;
SyntaxError: Identifier 'MongoClient' has already been declared at Loader.moduleStrategy (internal/modules/esm/translators.js:145:18) at async link (internal/modules/esm/module_job.js:64:21) [nodemon]
uj5u.com熱心網友回復:
由于您已經MongoClient在此處匯入:
import mongodb, { MongoClient } from 'mongodb';
^^^^^^^^^^^
here it is
……在這里再次匯入沒有意義:
const MongoClient = mongodb.MongoClient;
^^^^^^^^^^^
remove this
uj5u.com熱心網友回復:
import mongodb, { MongoClient } from 'mongodb';
這一行定義了兩個常量:mongodb和MongoClient。因此,當您嘗試創建另一個名為MongoClient.
const MongoClient = mongodb.MongoClient;
您應該能夠洗掉整條線并且它應該可以作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363260.html
上一篇:當錯誤的引數傳遞給API時,NodeJS應用程式崩潰
下一篇:在C#中創建一個新物件
