简体   繁体   English

Typescript应用程序中“已弃用mpromise(猫鼬的默认promise库)”

[英]“mpromise (mongoose's default promise library) is deprecated” in Typescript Application

How to resolve "mpromise (mongoose's default promise library) is deprecated" from a Typescript Application. 如何从Typescript应用程序中解决“不建议使用mpromise(猫鼬的默认诺言库)”。

I'm getting the following error: 我收到以下错误:

Left-hand side of assignment expression cannot be a constant or a read-only property. 赋值表达式的左侧不能为常量或只读属性。

I am using a MEAN stack with Angular 2 and would like to have mongoose use the bluebird promises library. 我正在Angular 2中使用MEAN堆栈,并希望猫鼬使用bluebird promises库。

I am getting an error when I attempt to follow these instructions on Stack Overflow and Mongo 当我尝试按照Stack OverflowMongo上的这些说明进行操作时出现错误

To be honest, I'm unsure if my issue is just lack of knowledge of Typescript or if I am doing something else wrong. 老实说,我不确定我的问题是否只是缺乏Typescript知识,还是我做错了其他事情。

"use strict";

import * as mongoose from 'mongoose';
var dbConst = require('../constants/db.json');
var bluebird = require("bluebird");

export class DBConfig {
    static init():void {
      const URL = (process.env.NODE_ENV === 'production') ? process.env.MONGOHQ_URL
                                                          : dbConst.localhost;

      mongoose.Promise = bluebird;     // <-- THIS IS WHERE ERROR OCCURS
      mongoose.connect(URL);
      mongoose.connection.on('error', console.error.bind(console, 'An error ocurred with the DB connection: '));
    }
};

I believe the following should work: 我相信以下应该起作用:

import * as mongoose from "mongoose";
import * as bluebird from "bluebird";

//either this
(<any>mongoose).Promise = bluebird;

//OR pass it in as an option
const connection = mongoose.createConnection("mongodb://localhost:27017", { 
    promiseLibrary: bluebird
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM