抛出新的错误('猫鼬未连接');

时间:2018-11-10 12:00:43

标签: javascript node.js mongodb mongoose

因此,我尝试将mongoose-gridfs模块与express一起使用,但是在尝试运行Express服务器时出现以下错误。

Error: Mongoose is not connected                                         
at new GridFSStorage (C:\Users\BARMAN\Documents\Codes\zubis-mart\node
_modules\mongoose-gridfs\lib\storage.js:43:11)                           
at module.exports (C:\Users\BARMAN\Documents\Codes\zubis-mart\node_mo
dules\mongoose-gridfs\lib\storage.js:472:18)                             
at Object.<anonymous> (C:\Users\BARMAN\Documents\Codes\zubis-mart\rou
tes\users.js:24:42)                                                      
at Module._compile (module.js:653:30)                                
at Object.Module._extensions..js (module.js:664:10)                  
at Module.load (module.js:566:32)                                    
at tryModuleLoad (module.js:506:12)                                  
at Function.Module._load (module.js:498:3)                           
at Module.require (module.js:597:17)                                 
at require (internal/module.js:11:18)                                
at Object.<anonymous> (C:\Users\BARMAN\Documents\Codes\zubis-mart\ser
ver.js:15:15)                                                            
at Module._compile (module.js:653:30)                                
at Object.Module._extensions..js (module.js:664:10)                  
at Module.load (module.js:566:32)                                    
at tryModuleLoad (module.js:506:12)                                  
at Function.Module._load (module.js:498:3)   

在这里我认为问题出在##

const express = require('express');
const bcrypt = require('bcryptjs');
const fs = require('fs');
const mongoose = require('mongoose');
const multer = require('multer');
const passport = require('passport');

const config = require('../config/database');

mongoose.connect(config.database, {
    useNewUrlParser: true
});

let conn = mongoose.connection;

conn.once('open', () => {
    console.log('Database File Upload Connection Established Successfully.');
});

conn.on('error', (err) => {
    console.log('File Connection Error... ' + err);
});

const gridfs = require('mongoose-gridfs')({
    collection: 'images',
    model: 'Image',
    mongooseConnection: conn
});

我的Connection字符串可以,但是我似乎无法弄清楚。真的需要帮助。谢谢你们!

2 个答案:

答案 0 :(得分:0)

您可能会从跟踪中看到,当它试图创建GridFSStorage实例并由于猫鼬尚未准备就绪而引发错误时,就会发生问题:

  

错误:猫鼬未连接
  在新的GridFSStorage (C:\ Users \ BARMAN \ Documents \ Codes \ zubis-mart \ node   _modules \ mongoose-gridfs \ lib \ storage.js:43:11)


在连接gridfs之前,确保猫鼬已连接到db。

在这里已解决:

const express = require('express');
const bcrypt = require('bcryptjs');
const fs = require('fs');
const mongoose = require('mongoose');
const mongooseGridFS = require('mongoose-gridfs');
const multer = require('multer');
const passport = require('passport');


const config = require('../config/database');

mongoose.connect(config.database, {
    useNewUrlParser: true
});

let conn = mongoose.connection;

conn.once('open', () => {
    console.log('Database File Upload Connection Established Successfully.');

    global.gridfs = mongooseGridFS({
      collection: 'images',
      model: 'Image',
      mongooseConnection: conn
    });
});

conn.on('error', (err) => {
    console.log('File Connection Error... ' + err);
});


P.S。我已经读过mongoose-gridfs manual,这完全暗示了您的问题,这是不正确的。
由于在代码源at line #43中,它会立即检查连接状态-这确实表明文档不正确。

答案 1 :(得分:0)

您应该确保先打开连接,然后初始化gridfs,

conn.once('open', () => {
    global.gridfs = mongooseGridFS({
      collection: 'images',
      model: 'Image',
      mongooseConnection: conn
    });
});