weixin_好的 2018-10-05 02:19 采纳率: 100%
浏览 937
已结题

mongoose+grid-stream出错求纠正:Error: missing db argument new Grid(db, mongo)

以下代 码 出错

var mongoose = require('mongoose');
var mongo = require('mongodb');
var Grid = require('gridfs-stream');
mongoose.Promise = global.Promise;
MongoClient:true});
//var conn = mongoose.createConnection('mongodb://127.0.0.1:27017/phprovince',{useNewUrlParser:true});
var conn = mongoose.connect('mongodb://localhost:27017/phprovince',{useNewUrlParser:true});
//var conn = new mongo.Db('phprovince', new mongo.Server("127.0.0.1", 27017));
var fs = require('fs');
Grid.mongo = mongoose.mongo;
var gfs = Grid(conn.db);
var path = require('path');
var util = require('util');
var urlencode = require('urlencode');
var mongooseSchema = new mongoose.Schema({
filename: String,
metadata: String,
aliases: String
},
{collection: "fs.files",
versionKey: ""}

);
var getFile = conn.model('getFile', mongooseSchema);
var fileUpload = {};

错误信息如下:
C:\Program Files\nodejs\xlsxtomongod\node_modules\gridfs-stream\lib\index.js:25
if (!db) throw new Error('missing db argument\nnew Grid(db, mongo)');
^

Error: missing db argument
new Grid(db, mongo)
at new Grid (C:\Program Files\nodejs\xlsxtomongod\node_modules\gridfs-stream\lib\index.js:25:18)
at Grid (C:\Program Files\nodejs\xlsxtomongod\node_modules\gridfs-stream\lib\index.js:19:12)
at Object. (C:\Program Files\nodejs\xlsxtomongod\gridfs.js:12:11)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
启用var conn = new mongo.Db('phprovince', new mongo.Server("127.0.0.1", 270
17));
出错信息如下:
========================================================================================
= Please ensure that you set the default write concern for the database by setting =
= one of the options =
= =
= w: (value of > -1 or the string 'majority'), where < 1 means =
= no write acknowledgement =
= journal: true/false, wait for flush to journal before acknowledgement =
= fsync: true/false, wait for flush to file system before acknowledgement =
= =
= For backward compatibility safe is still supported and =
= allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}] =
= the default value is false which means the driver receives does not =
= return the information of the success/error of the insert/update/remove =
= =
= ex: new Db(new Server('localhost', 27017), {safe:false}) =
= =
= http://www.mongodb.org/display/DOCS/getLastError+Command =
= =
= The default of no acknowledgement will change in the very near future =
= =

= This message will disappear when the default safe is set on the driver Db =

C:\Program Files\nodejs\xlsxtomongod\gridfs.js:25
var getFile = conn.model('getFile', mongooseSchema);
^

TypeError: conn.model is not a function
at Object. (C:\Program Files\nodejs\xlsxtomongod\gridfs.js:25:20)

展开全部

  • 写回答

2条回答 默认 最新

  • devmiao 2018-10-10 08:29
    关注
    评论
  • hi_zf 2020-05-08 01:40
    关注

    const mongoose = require('mongoose');
    const conn = mongoose.connection
    const gfs = Grid(conn)

    用conn替换db , 翻墙貌似容易找到答案
    https://github.com/aheckmann/gridfs-stream/issues/131

    ---------------分割线--------
    上面的不能完全解决问题 , 使用下面的可以
    var gfs;
    conn.once('open', () => {
    gfs = Grid(conn.db, mongoose.mongo);
    })

    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 编写Linux测试服务启动失败
  • ¥20 matlab绘冲床平面连杆机构图和仿真,求帮助
  • ¥15 为什么树莓派5b显示禁止连接
  • ¥15 请专家处理报错!基于深度学习的车型分类问题:数据集为包含 10 种车型的图像数据集,分为训练集、验证集和测试集。
  • ¥20 流量太费!寻找便宜的app音视频SDK或平替方案。
  • ¥15 kubeasz部署遇到问题
  • ¥15 GUIDE to App Designer Migration Tool for MATLAB
  • ¥50 第三代非支配排序遗传算法(NSGA-Ⅲ)和多目标粒子群优化算法(MOPSO)的实现
  • ¥20 plant simulation与python com接口实时数据交互
  • ¥15 有关汽车的MC9S12XS128单片机实验
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部