濮阳杆衣贸易有限公司

主頁 > 知識庫 > 關(guān)于mongoose連接mongodb重復(fù)訪問報錯的解決辦法

關(guān)于mongoose連接mongodb重復(fù)訪問報錯的解決辦法

熱門標(biāo)簽:地圖標(biāo)注什么軟件好用 地圖標(biāo)注百度競價 神行者百貨商場地圖標(biāo)注 外呼系統(tǒng)線路經(jīng)常出問題嗎 如何辦理400客服電話 安陽手機(jī)自動外呼系統(tǒng)原理是什么 西藏地圖標(biāo)注改進(jìn)點(diǎn) 地圖標(biāo)注專員入駐 外呼系統(tǒng)怎樣才能不封號

具體代碼如下所示:

var express = require('express');
var mongoose = require('mongoose');
var router = express.Router();
var Person = mongoose.model('Person',{
 id:Number,
 name:String
});
/*新增*/
router.get('/insert', function(req, res){
 var student = new Person({
  id:1,
  name:"huop"
 });
 mongoose.connect("mongodb://localhost:27017/test");
 student.save(function(e, product, numberAffected) {
  if (e) res.send(e.message);
  var html = "
新增的數(shù)據(jù)為:" + JSON.stringify(product);
  html += "
影響的數(shù)據(jù)量為:" + numberAffected;
  res.send(html);
 });
});
router.get('/find',function(request, response){
 mongoose.connect("mongodb://localhost:27017/test");
 Person.find({
  id: 1
 }, function(e, docs) {
  if (e) response.send(e.message);
  var html = "
 查詢到的數(shù)據(jù)為:" + JSON.stringify(docs);
  response.send(html);
 });
});

以上代碼可以正常執(zhí)行,但是有個問題,就是第一次訪問的時候正常,但是刷新一遍就會報錯,Trying to open unclosed connection.undefined
Error: Trying to open unclosed connection.

應(yīng)該是重復(fù)打開連接失敗報錯,所以需要關(guān)閉連接,我加了一段關(guān)閉連接的代碼,結(jié)果查詢結(jié)果就成了:查詢到的數(shù)據(jù)為:unde

那么該如何處理mongodb數(shù)據(jù)庫連接?

解決辦法如下所示:

新建一個mongoose.js:

var mongoose = require(‘mongoose');
mongoose.connect(‘mongodb://localhost/nuaidibi');
module.exports = mongoose;

每個module中,引用

var mongoose = require('./mongoose.js');下面使用就一樣了,不用開關(guān)連接。

您可能感興趣的文章:
  • MongoDB啟動報錯 28663 Cannot start server
  • MongoDB添加仲裁節(jié)點(diǎn)報錯:replica set IDs do not match的解決方法
  • NodeJS連接MongoDB數(shù)據(jù)庫時報錯的快速解決方法
  • perl操作MongoDB報錯undefined symbol: HeUTF8解決方法
  • Mongodb 崩潰報錯 Too many open files的問題解析

標(biāo)簽:萍鄉(xiāng) 酒泉 張掖 衡水 貴港 阜陽 雞西 AXB

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《關(guān)于mongoose連接mongodb重復(fù)訪問報錯的解決辦法》,本文關(guān)鍵詞  關(guān)于,mongoose,連接,mongodb,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《關(guān)于mongoose連接mongodb重復(fù)訪問報錯的解決辦法》相關(guān)的同類信息!
  • 本頁收集關(guān)于關(guān)于mongoose連接mongodb重復(fù)訪問報錯的解決辦法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    永平县| 桦川县| 华宁县| 江华| 南华县| 张家口市| 卢氏县| 杭锦后旗| 双桥区| 九龙城区| 嘉荫县| 集贤县| 九龙县| 延津县| 灵武市| 崇仁县| 新民市| 恩施市| 和顺县| 普定县| 固安县| 奉节县| 松江区| 汾阳市| 南木林县| 大洼县| 江门市| 德保县| 荣成市| 双辽市| 平乡县| 新宾| 揭西县| 隆安县| 内丘县| 资阳市| 蒲城县| 奇台县| 新绛县| 睢宁县| 兴城市|