gpt4 book ai didi

javascript - mongodb 在保存数据时抛出错误(拓扑被破坏)

转载 作者:行者123 更新时间:2023-11-30 07:57:40 25 4
gpt4 key购买 nike

我正在尝试使用 Node.js 将数据写入 MongoDB在写入数据时,我在最后一行收到以下错误。执行日志是:

{ _id: 56e90c1292e69900190954f5, 
nfs: [ 'ebdp1', 'ebdp2', 'ebdp3', 'ebdp4' ],
snapShotTime: '2016-03-16 07:32:34' }
{ [MongoError: topology was destroyed] name: 'MongoError', message: 'topology was destroyed' }

我的代码结构是根据框架,其中 Collection 的模式在 appTableProdSchema.js 文件中,而对象数据在 appTableProdData.js 文件中。Main 是 newMain.js

代码如下:newMain.js

var mongoose = require('mongoose');
var moment = require('moment');
var nfs_check="";
var promises = [];

var nodes = ["ebdp1","ebdp2", "ebdp3", "ebdp4"];

mongoose.connect('mongodb://localhost:27017/test');
var db = mongoose.connection;
var storageData = require('./appTableProdData.js');
var storageDataSchema = require('./appTableProdSchema.js');
var obj = {};
obj.snapShotTime = moment().utc().format("YYYY-MM-DD HH:mm:ss");
obj.nfs = nodes;
db.once('open', function() {
var mongoData = new storageData();
mongoData.fill(obj);
var promise = new storageDataSchema(mongoData.getInformation());
console.log(promise.toString());
promise.save(function (err, promise) {
if (err) return console.error(err);
promise.speak();
});
});
mongoose.disconnect();

appTableProdSchema.js

var mongoose = require('mongoose');

module.exports = mongoose.model('AppTableProd', {
snapShotTime : String,
nfs: Array
});

appTableProdData.js

var storageData = function() {
this.data = {
snapShotTime: null,
nfs: null
};
this.fill = function(resp) {
this.data.snapShotTime = resp.snapShotTime;
this.data.nfs = resp.nfs;
};
this.getInformation = function() {
return this.data;
};
};
module.exports = storageData;

知道为什么我会收到 topology destroyed 错误吗?请帮忙。

最佳答案

你的

mongoose.disconnect();

之前被调用过

db.once('open', function() {
var mongoData = new storageData();
mongoData.fill(obj);
var promise = new storageDataSchema(mongoData.getInformation());
console.log(promise.toString());
promise.save(function (err, promise) {
if (err) return console.error(err);
promise.speak();
});
});

此代码块正在执行,因为它是一个异步调用。

你应该这样称呼

mongoose.disconnect();

像这样

 db.once('open', function() {
var mongoData = new storageData();
mongoData.fill(obj);
var promise = new storageDataSchema(mongoData.getInformation());
console.log(promise.toString());
promise.save(function (err, promise) {
if (err) return console.error(err);
promise.speak();
mongoose.disconnect();
});
});

关于javascript - mongodb 在保存数据时抛出错误(拓扑被破坏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36029595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com