gpt4 book ai didi

node.js - 当我使用 mongoose 时,我收到一个错误,指出未找到 collectionNames 函数

转载 作者:太空宇宙 更新时间:2023-11-04 03:07:25 25 4
gpt4 key购买 nike

在完成 native mongo 命令之后,我尝试了 ODM,即 mongoose,它扩展了 MongoDB 的功能。

我首先从本地主机上的现有数据库中简单地获取我的集合。我能够连接到我的数据库,但无法获取集合。然而我没能这样做。

 

/**
* Uses MongooseJS to Connect to MongoDB
* .Maps out all collections within
*/

var mongoose = require('mongoose')
, MONGO_DB = 'mongodb://localhost/astro';

mongoose.connect(MONGO_DB);

mongoose.connection.on('open', function(){
mongoose.connection.db.collectionNames(function(error, collections) {
if (error) {
throw new Error(error);
} else {
collections.map(function(collection) {
console.log('found collection %s', collection.name);
});
}
});
});

mongoose.connection.on('error', function(error){
throw new Error(error);
});

TypeError: mongoose.connection.db.collectionNames is not a function
at NativeConnection.<anonymous> (/Users/sagarmunjal/Desktop/untitled folder/script.js:29:26)
at emitNone (events.js:80:13)
at NativeConnection.emit (events.js:179:7)
at open (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongoose/lib/connection.js:511:11)
at NativeConnection.Connection.onOpen (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongoose/lib/connection.js:520:5)
at /Users/sagarmunjal/Desktop/untitled folder/node_modules/mongoose/lib/connection.js:476:11
at /Users/sagarmunjal/Desktop/untitled folder/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:60:5
at /Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb/lib/db.js:227:5
at connectHandler (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb/lib/server.js:286:7)
at g (events.js:273:16)
at emitOne (events.js:90:13)
at emit (events.js:182:7)
at /Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb-core/lib/topologies/server.js:504:23
at commandCallback (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb-core/lib/topologies/server.js:936:9)
at Callbacks.emit (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb-core/lib/topologies/server.js:116:3)
at null.messageHandler (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb-core/lib/topologies/server.js:282:23)

/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongodb/lib/server.js:289 process.nextTick(function() { 抛出错误; }) ^

类型错误:mongoose.connection.db.collectionNames 不是函数 在 NativeConnection 处。 (/Users/sagarmunjal/Desktop/无标题文件夹/script.js:29:26) 在emitNone(events.js:80:13) 在 NativeConnection.emit (events.js:179:7) 打开时(/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongoose/lib/connection.js:511:11) 在 NativeConnection.Connection.onOpen (/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongoose/lib/connection.js:520:5) 在/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongoose/lib/connection.js:476:11 在/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:60:5 在/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongodb/lib/db.js:227:5 在 connectHandler (/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongodb/lib/server.js:286:7) 在 g (events.js:273:16) 在emitOne (events.js:90:13) 在发出时(events.js:182:7) 在/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongodb-core/lib/topologies/server.js:504:23 在 commandCallback (/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongodb-core/lib/topologies/server.js:936:9) 在 Callbacks.emit (/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongodb-core/lib/topologies/server.js:116:3) 在 null.messageHandler (/Users/sagarmunjal/Desktop/无标题文件夹/node_modules/mongodb-core/lib/topologies/server.js:282:23)[nodemon] 应用程序崩溃 - 启动前等待文件更改...

最佳答案

mongoose.connection.db.collectionNames 已被弃用。我建议改用mongoose.connection.db.listCollections()

查看另一篇 stackoverlow 帖子 here

关于node.js - 当我使用 mongoose 时,我收到一个错误,指出未找到 collectionNames 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com