gpt4 book ai didi

javascript - 更改为 Bluebird Promise,但使用 mongoose 仍然收到弃用警告,为什么?

转载 作者:行者123 更新时间:2023-12-03 04:51:13 26 4
gpt4 key购买 nike

为什么我仍然收到已弃用的警告,我已尝试遵循 bluebird 手册,但仍然无法使其工作。

如果我按照 mongoose Promise 说明进行操作,我也会收到错误:

assert.equal(query.exec().constructor, require('bluebird'));
^

ReferenceError: assert is not defined
at Object.<anonymous>

使用时:

//使用 Bluebird

mongoose.Promise = require('bluebird');
assert.equal(query.exec().constructor, require('bluebird'));

错误:

node:10059) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
(node:10059) Warning: Possible EventEmitter memory leak detected. 11 close listeners added. Use emitter.setMaxListeners() to increase limit

代码

var mongoose = require('mongoose');
var Promise = require('bluebird');
Promise.promisifyAll(require('mongoose'));

var _ = require('lodash'),
fs = require('fs'),
path = require('path');

mongoose.connect('mongodb://localhost/eclass');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {
// we're connected!
// create db schema
var EclassSchema = new mongoose.Schema({
xsi: {
xsitype: 'string',
id: 'string'
},
date_of_original_definition: 'string',
date_of_current_version: 'string',
date_of_current_revision: 'string',
revision: 'string',
status: 'string',
source_language: {
country_code: 'string',
language_code: 'string'
},
preferred_name: 'string',
definition: 'string',
its_superclass: 'string',
hierarchical_position: 'string',
//keywords: 'string'
});
// Create model
var Eclass = mongoose.model('Eclass', EclassSchema);

const pjsons = path.join(__dirname, '/../', 'file-operations', 'json-files');
//console.log(pjsons);

function readFiles(pjsons, onError) {
fs.readdir(pjsons, function(err, filenames) {
if(err) {
onError(err);
return;
}

filenames.forEach(function(filename) {
fs.readFile(pjsons + '/' + filename, 'utf-8', function(err, data) {
if(err) {
onError(err);
return;
}
data = JSON.parse(data);
// Digging down into the json code
const ontomlOntoml = data['dic:eclass_dictionary']['ontoml:ontoml'];
const onto = _.first(ontomlOntoml);
const dictionary = onto['dictionary'];
const contClasses = _.first(dictionary);
const containedClasses = contClasses['contained_classes'];
const ontClass = _.first(containedClasses);
const ontomlClass = _.find(ontClass);

//Arrays
const xsiArray = _.map(ontomlClass, '$');
const date_of_original_definitionArray = _.map(ontomlClass, 'date_of_original_definition');
const date_of_current_versionArray = _.map(ontomlClass, 'date_of_current_version');
const date_of_current_revisionArray = _.map(ontomlClass, 'date_of_current_revision');
const revisionArray = _.map(ontomlClass, 'revision');
const statusArray = _.map(ontomlClass, 'status');
const sourceLanguageArray = _.map(ontomlClass, 'source_language');
const preferredNameArray = _.map(ontomlClass, 'preferred_name');
const definitionArray = _.map(ontomlClass, 'definition');
const itsSuperclassArray = _.map(ontomlClass, 'its_superclass');
const hierarchical_positionArray = _.map(ontomlClass, 'hierarchical_position');
//const keywordsArray = _.map(ontomlClass, 'keywords');

// Looping and storing the data into mongodb
//console.log(ontomlClass.length);
for (var i = 0; i < ontomlClass.length; i++) {
//console.log(hierarchical_positionArray[i]);
var newEclass = new Eclass();
newEclass.xsi.xsitype = xsiArray[i]['xsi:type'];
newEclass.xsi.id = xsiArray[i]['id'];
newEclass.date_of_original_definition = date_of_original_definitionArray[i];
newEclass.date_of_current_version = date_of_current_versionArray[i];
newEclass.date_of_current_revision = date_of_current_revisionArray[i];
newEclass.revision = revisionArray[i];
newEclass.status = statusArray[i];
newEclass.source_language.country_code = sourceLanguageArray[i][0].$.country_code;
newEclass.source_language.language_code = sourceLanguageArray[i][0].$.language_code;
newEclass.preferred_name = preferredNameArray[i][0].label[0]._;
newEclass.definition = definitionArray[i][0].text[0]._;
//newEclass.its_superclass = itsSuperclassArray[i][0].$.class_ref;
newEclass.hierarchical_position = hierarchical_positionArray[i];
//newEclass.keywords = keywordsArray[i][0].label[0]._;
newEclass.saveAsync()
.then(function() {
mongoose.disconnect();
})
.catch(function(err) {
console.log('There was an error', err);
});
}
});
});
});
}
readFiles(pjsons);
});

最佳答案

我得到了 @Bergi 的帮助来解决这个问题。

更改自:

var mongoose = require('mongoose');
var Promise = require('bluebird');
Promise.promisifyAll(require('mongoose'));

至:

mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

更改自

.saveAsync()

.save()

关于javascript - 更改为 Bluebird Promise,但使用 mongoose 仍然收到弃用警告,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42652410/

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