SU7*_*SU7 16 mongoose mongodb node.js typescript
我的 GitHub 存储库: https: //github.com/safiullah7/legan
分支:redux
我正在关注本教程:https://tomanagle.medium.com/build-a-rest-api-with-node-js-typescript-mongodb-b6c898d70d61 ,但我无法连接到我的 mongodb。这是我尝试连接 mongodb 的代码文件:
import config from "config";
import log from "../logger";
function connect() {
const dbUri = config.get("dbUri") as string;
return mongoose
.connect(dbUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
log.info("Database connected");
})
.catch((error) => {
log.error("db error", error);
process.exit(1);
});
}
export default connect;
Run Code Online (Sandbox Code Playgroud)
编译器给出以下错误:
No overload matches this call.
Overload 1 of 3, '(uri: string, callback: CallbackWithoutResult): void', gave the following error.
Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; }' is not assignable to parameter of type 'CallbackWithoutResult'.
Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'CallbackWithoutResult'.
Overload 2 of 3, '(uri: string, options?: ConnectOptions | undefined): Promise<typeof import("mongoose")>', gave the following error.
Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; }' is not assignable to parameter of type 'ConnectOptions'.
Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'ConnectOptions'.
Run Code Online (Sandbox Code Playgroud)
我是打字稿和节点/mongoos 的新手。将不胜感激您的帮助。
小智 32
您可以将 mongoose.connect 函数更改为:
mongoose.connect(dbUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
} as ConnectOptions)
Run Code Online (Sandbox Code Playgroud)
这是最简单的方法
小智 22
新版本mongoose(我写这篇文章时的最新版本是6.0.2)具有以下函数的类型定义connect()。
/** Opens Mongoose's default connection to MongoDB, see [connections docs](https://mongoosejs.com/docs/connections.html) */
export function connect(uri: string, options: ConnectOptions, callback: CallbackWithoutResult): void;
export function connect(uri: string, callback: CallbackWithoutResult): void;
export function connect(uri: string, options?: ConnectOptions): Promise<Mongoose>;
Run Code Online (Sandbox Code Playgroud)
您options object传递给connect()函数的
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
Run Code Online (Sandbox Code Playgroud)
因此,应该具有 的类型ConnectOptions。
当前的定义ConnectOptions如下:
interface ConnectOptions extends mongodb.MongoClientOptions {
/** Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection. */
bufferCommands?: boolean;
/** The name of the database you want to use. If not provided, Mongoose uses the database name from connection string. */
dbName?: string;
/** username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility. */
user?: string;
/** password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility. */
pass?: string;
/** Set to false to disable automatic index creation for all models associated with this connection. */
autoIndex?: boolean;
/** Set to `true` to make Mongoose automatically call `createCollection()` on every model created on this connection. */
autoCreate?: boolean;
}
Run Code Online (Sandbox Code Playgroud)
查看 and 的新定义connect,我们可以看到内部ConnectOptions没有useNewUrlParseror的定义。这就是我们收到此类错误的原因。您可以删除这些选项,并且您的代码应该能够连接到您的 MongoDB。如果您想向函数传递一些选项,那么它们应该遵循.useUnifiedTopologyConnectOptionsuseNewUrlParser: true,useUnifiedTopology: true,connect()ConnectOptions
Ale*_*rto 20
更新:澄清这个答案作为未来读者的参考。
如果您使用 Mongoose v6+,则此答案适用。
其他答案解释了如何扩展打字稿以接受 useNewUrlParser 和其他选项,这将解决原始问题。
但是,根据 Mongoose 文档,如果您使用 Mongoose v6+,则不再需要这些选项,因为它们已作为默认值包含在内。
根据 Mongoose 版本 6 迁移指南:
useNewUrlParser、useUnifiedTopology、useFindAndModify 和 useCreateIndex 不再受支持的选项。Mongoose 6 的行为始终如同 useNewUrlParser、useUnifiedTopology 和 useCreateIndex 为 true,而 useFindAndModify 为 false 一样。请从您的代码中删除这些选项。
https://mongoosejs.com/docs/migration_to_6.html#no-more-deprecation-warning-options
因此,不再需要包含这些选项。我想这就是为什么它们不再包含在 ConnectOptions 类型中的原因。
如果您使用的是旧版本的 Mongoose,请参阅其他答案以了解如何扩展 ConnectOptions 类型以允许这些选项。
| 归档时间: |
|
| 查看次数: |
27130 次 |
| 最近记录: |