[英]How to implement room to room file sharing system using socket.io and nodejs larger than 1mb?
[英]getting err in file sharing system using nodejs,express mongoose
這是我的 nodejs 代碼
const mongoose = require('mongoose');
function connectDB() {
//Database connection
mongoose.connect(process.env.MONGO_CONNECTION_URL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: true
});
const connection = mongoose.connection;
connection.once('open', () => {
console.log('Database connected.');
}).catch(err => {
console.log('Connection failed.');
})
}
module.exports = connectDB;
在我嘗試與服務器連接時顯示此錯誤
}).catch(err => {
^
TypeError: connection.once(...).catch is not a function
它會返回一個承諾嗎?
db.once('open', function(err, resp){
console.log(resp);
});
或者
mongoose.connection
.once('open', function () {
console.log('MongoDB running');
})
.on('error', function (err) {
console.log(err);
});
這意味着once
函數首先不返回承諾對象。
如果有錯誤,您可以使用catch
和mongoose.connect
來處理錯誤,請查看下面的示例:
function connectDB() {
//Database connection
mongoose.connect(
process.env.MONGO_CONNECTION_URL,
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: true
}
).catch(err => console.log(err.reason));;
const connection = mongoose.connection;
connection.once('open', () => {
console.log('Database connected.');
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.