javascript - mongodb使用mongoose没有办法链接,但是软件Robomongo和命令mongo都可以连接
PHP中文网
PHP中文网 2017-04-11 11:52:27
[JavaScript讨论组]

问题如题。写了个小程序,突然之间就没有办法连上了,之前还可以的。
mongodb 和程序都是在本地mongodb 是用brew 安装的。网上查了好多,还是没找到原因,数据库的文件夹也重新删除过,还是不管用。求老司机指导,贴一下代码吧。

let mongoose = require("mongoose");
let utils = require("./utils");
var log4js = require('log4js');
//log the  logger messages to a file, and the console ones as well.
log4js.configure({
    appenders: [
        {
            type: "file",
            filename: "log/trace_all.log",
        },
        {
            type: "console"
        }
    ],
    replaceConsole: true
});
//regist model 
require("./models/comment");
let CommentModel = mongoose.model("Comment");

mongoose.connect("mongodb://localhost:27017/acfun");
let db = mongoose.connection;
db.once("open", () => {
    console.log("mongoose connect mongodb://localhost:27017/acfun success");
});
db.on("error", (err) => {
    console.log(`db error : ${err}`);
    setTimeout(()=>{
        mongoose.connect("mongodb://localhost:27017/acfun");
    },500);
});
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(2)
巴扎黑
  1. 数据库启动了嘛?

天蓬老师

我的连接方法这样的...没问题

let mongoose = require('mongoose')
let conStr = 'mongodb://127.0.0.1/app'

mongoose.Promise = global.Promise

/*mongoose.set('debug', true)*/

const conn = mongoose.createConnection(conStr)

mongoose.connection.on('error', console.error.bind(console, '数据库连接错误\n'))
mongoose.connection.on('connected', console.log.bind(console, '成功连接到数据库\n\n\n'))

module.exports = conn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号