2015-04-04 19 views
11

Tôi đang cố kết nối với mongodb bằng cách sử dụng nodejs và socket.io. Tôi có thể kết nối với cơ sở dữ liệu bởi vì tôi nhận được 'kết nối được chấp nhận trong giao diện điều khiển của tôi, nhưng ở phía bên nodejs, ngay sau khi tôi - thực sự - có được'process.nextTick (function() {throw err;})' - Undefined không phải là hàm (mongodb/mongoose)

Connection to mongodb://localhost:27017 established through mongoose

nó ngay lập tức thất bại tiếp theo với

process.nextTick(function() { throw err; }) ^TypeError: undefined is not a function at showCollections**

Và đây đi showCollections:

var showCollections = function(db, callback) { 
    mongoose.connection.db.collectionNames(function(error, names) { 
    if (error) { 
     throw new Error(error); 
    } else { 
     console.log("=>Listening mongo collections:"); 
     names.map(function(cname) { 
     mongoose.connection.db.dropCollection(cname.name); 
     console.log("--»"+cname.name); 
     }); 
    } 
    }); 

} 

Và đây là nội dung của thư mục cơ sở dữ liệu của tôi:

_tmp (empty folder) 
local.0 
local.ns 
mongod.lock 

Tôi chạy mongodb bằng cách nhập mongod --dbpath thư mục và thành công 'đang chờ kết nối trên cổng 27017'.

Ngoài ra, node_modules tôi từ package.json (NPM)

"dependencies": { 
    "express": "^4.9.6", 
    "socket.io": "latest", 
    "mongodb": "~2.0", 
    "mongoose": "*" 
    } 

Cảm ơn bạn rất nhiều vì sự giúp đỡ của bạn ...

StackTrace:

> TypeError: undefined is not a function 
>  at showCollections (/usr/share/nginx/www/index.js:77:25) 
>  at NativeConnection.callback (/usr/share/nginx/www/index.js:46:3) 
>  at NativeConnection.g (events.js:199:16) 
>  at NativeConnection.emit (events.js:104:17) 
>  at open (/usr/share/nginx/www/node_modules/mongoose/lib/connection.js:485:10) 
>  at NativeConnection.Connection.onOpen (/usr/share/nginx/www/node_modules/mongoose/lib/connection.js:494:5) 
>  at /usr/share/nginx/www/node_modules/mongoose/lib/connection.js:453:10 
>  at /usr/share/nginx/www/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:59:5 
>  at /usr/share/nginx/www/node_modules/mongoose/node_modules/mongodb/lib/db.js:200:5 
>  at connectHandler (/usr/share/nginx/www/node_modules/mongoose/node_modules/mongodb/lib/server.js:272:7) 

EDIT:

Tôi cũng có những vấn đề khi cố gắng chạy dụ nodejs:

{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } 
js-bson: Failed to load c++ bson extension, using pure JS version 

Tôi đã cố gắng sửa chữa chúng như câu hỏi khác ở đây sẽ nói nhưng không làm việc hoặc ...

+0

Bạn có thể đăng theo dõi ngăn xếp thực tế được in ra không? Điều đó sẽ cho bạn biết * trong đó * trong 'showCollections()' lỗi xuất phát từ đó. – mscdex

+0

@mscdex làm cách nào để in dấu vết ngăn xếp? nó có được in theo mặc định không? – Fane

+0

Có, nó phải được hiển thị dưới TypeError mà bạn có. – mscdex

Trả lời

25

Từ những thông tin được cung cấp, có vẻ như bạn đang sử dụng MongoDB tài xế 2.0 . Phương thức db.collectionNames đã bị loại bỏ. Xem phần "Đối tượng Db" của trang này - https://github.com/mongodb/node-mongodb-native/blob/0642f18fd85037522acf2e7560148a8bc5429a8a/docs/content/tutorials/changes-from-1.0.md#L38

Họ đã thay thế bằng danh sáchCollections. Bạn sẽ nhận được hiệu ứng tương tự với:

mongoose.connection.db.listCollections().toArray(function(err, names) { 
    if (err) { 
     console.log(err); 
    } 
    else { 
     names.forEach(function(e,i,a) { 
      mongoose.connection.db.dropCollection(e.name); 
      console.log("--->>", e.name); 
     }); 
    } 
}); 
+0

Trong trường hợp nó giúp: Tôi đã cố gắng sử dụng 'listCollectionsAsync()' sau khi sử dụng 'Promise.promisifyAll (mongoose.connection.db)' + package 'asyncawait', không thành công (treo khi gọi' listCollectionsAsync() ' không có lý do). Tuy nhiên sau khi một 'console.log (mongoose.connection.db)' kiểm tra các phương thức có sẵn, tôi đã tìm thấy một 'collectionsAsync()' không có giấy tờ hoạt động hoàn hảo O_o (mongoose 4.3.7) –

0

Không nên bạn chỉ định đường dẫn đầy đủ và có mô-đun xuất?
... cái gì đó như:

mongoose.connection.db.collectionNames(function (err, names) 
{ 
     console.log(names); // [{ name: 'dbname.myCollection' }] 
     module.exports.Collection = names; 
} 

Nếu tôi sai đó là bởi vì tôi không ưa thích moongodb :)

+0

Tôi không thực sự nghĩ rằng điều này là cần thiết, nếu tôi có quan điểm của bạn ... Tôi không nghĩ rằng vấn đề của tôi nằm ở đó ... – Fane

+0

Kết quả là gì nếu bạn chạy db.getCollectionNames()? –

+0

giống nhau ... vì vậy vấn đề có thể là lấy tên bộ sưu tập, vì bất kỳ lý do gì! – Fane

Các vấn đề liên quan