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 ...
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
@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
Có, nó phải được hiển thị dưới TypeError mà bạn có. – mscdex