下面是我用來連接到 mongodb 的代碼。
import { MongoClient } from "mongodb"
const user = 'user_001'
const userPassword = '<password>'
const cluster = 'cluster0.1jjgj'
const url = `mongodb srv://${user}:${userPassword}@${cluster}.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`
export const connectDatabase = async() => {
const client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = client.db('main')
return {
listings: db.collection('test-listings')
}
}
嘗試將 mongodb 與 MongoDB NodeJS 驅動程式連接,mongodb但發生以下錯誤。MongoClientOptions (useNewUrlParser: boolean, useUnifiedTopology:boolean) 在 mongodb 中做了什么,什么可能導致這個錯誤?請解釋。謝謝你。
Overload 1 of 4, '(url: string, callback: Callback<MongoClient>): void', gave the following error.
Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; }' is not assignable to parameter of type 'Callback<MongoClient>'.
Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'Callback<MongoClient>'.
Overload 2 of 4, '(url: string, options: MongoClientOptions): Promise<MongoClient>', gave the following error.
Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; }' is not assignable to parameter of type 'MongoClientOptions'.
Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'MongoClientOptions'.ts(2769)
uj5u.com熱心網友回復:
您應該能夠通過以下方式連接到客戶端:
export const connectDatabase = async () => {
const client = new MongoClient(url);
await client.connect();
const db = client.db('main');
return {
listings: db.collection('test-listings'),
};
};
uj5u.com熱心網友回復:
這作業得很好,強調 poolSize。
mongoose.connect(
dbUrl,
{
useNewUrlParser: true,
useCreateIndex: true,
autoIndex: true,
useUnifiedTopology: true,
poolSize: 10,
},
function (err) {
if (err) {
console.log(`Unable to Connect to Database. ${err.name} ${err.message}`);
process.exit(1);
} else {
console.log(new Date());
console.error("Successfully connected to Database");
}
}
);
有關更多資訊,請查看https://www.mongodb.com/community/forums/t/argument-of-type-usenewurlparser-boolean-useunifiedtopology-boolean-is-not-assignable-to-parameter-of-type/119731/ 4
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/398825.html
