改为Bluebird Promise,但仍然使用mongoose获得弃用警告,为什么?

时间:2017-03-07 15:41:30

标签: javascript mongodb mongoose bluebird disconnect

为什么我仍然得到不赞成的警告,我试图按照蓝鸟手册,但仍然无法让它工作。

如果我遵循mongoose Promise说明,我也会收到错误:

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

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

使用时:

//使用蓝鸟

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);
});

1 个答案:

答案 0 :(得分:1)

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

更改自:

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

为:

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

改变
.saveAsync()

.save()