简体   繁体   English

如何用es6方式导入mongoose-long插件

[英]How to import mongoose-long plugin with es6 way

I want to convert following import syntax into es6 import syntax我想将以下导入语法转换为 es6 导入语法

const mongoose = require('mongoose')
require('mongoose-long')(mongoose);
const {Types: {Long}} = mongoose;

I don't normally use the ES6 syntax for modules so I consulted this article which seemed to be correct: https://codeburst.io/understanding-es6-modules-import-export-syntax-in-javascript-6c01f20cead3我通常不将 ES6 语法用于模块,所以我查阅了这篇似乎是正确的文章: https : //codeburst.io/understanding-es6-modules-import-export-syntax-in-javascript-6c01f20cead3

For your example this appeared to work for me (in my debugger I see the variable Long get a value and I can use it to create a Long value)对于您的示例,这似乎对我有用(在我的调试器中,我看到变量 Long 获得了一个值,我可以使用它来创建一个 Long 值)

import mongoose from 'mongoose'
import mongooseLong from 'mongoose-long'
mongooseLong(mongoose)
const {Types: {Long}} = mongoose;
const myVal = new Long(0, 0xffff)
console.log(myVal)

The only tricky step was handling require('mongoose-long')(mongoose) .唯一棘手的步骤是处理require('mongoose-long')(mongoose) I introduced the variable mongooseLong to work around that one liner.我引入了变量mongooseLong来解决那个班轮。

For this to be executable I needed to add the following to my package.json (this is mentioned in the linked article):为了使其可执行,我需要将以下内容添加到我的 package.json 中(链接文章中提到了这一点):

"type": "module"

When I run this program I see the following当我运行这个程序时,我看到以下内容

> node index.js 
Long { _bsontype: 'Long', low_: 0, high_: 65535 }

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

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