使用导入而不是 require() 时带有 uuid 的 MongooseError
MongooseError with uuid when using import instead of require()
我收到以下错误:
MongooseError: document must have an _id before saving
当我尝试使用 API 使用 uuid
创建一个对象(Campagne)时:
import uuidv4 from 'uuid/v4';
当我使用 :
时有效
const uuidv4 = require('uuid/v4');
我的 Campagne 对象使用 uuid
.
正确创建
这是我的对象架构的完整代码:
import * as mongoose from 'mongoose';
import uuidv4 from 'uuid/v4';
export const CampagneSchema = new mongoose.Schema({
_id: { type: String, default: uuidv4 },
dateDebut: Date,
dateFin: Date,
reduction: Number,
});
TSLint 告诉我使用 import
而不是 require()
并在我的 IDE 中将其作为错误下划线,但它肯定无法正常工作,如上所示。
有人能解释一下为什么会这样吗?
有关信息,我使用带有 Typescript 的 NestJS node.js 框架。
澄清:
我想知道为什么 import
适用于 mongoose
但不适用于 uuid
(require
适用于 uuid
)
删除_id: { type: String, default: uuidv4 },
Mongoose 会自动生成_id
并使用const ddd = require(...)
我认为 ES6 模块在 Node 项目中不能正常工作
我在 Github node-uuid
上找到了问题的答案。
以下代码有效:
import {v4 as uuid} from 'uuid';
https://github.com/kelektiv/node-uuid/issues/245
import uuid from 'uuid/v4';
syntax does not work, at least in a Typescript v3
project running on Node v10.9.0
(without webpack nor babel, only ts-node
to compile/run Typescript)
I get the following error: TypeError: v4_1.uuid is not a function
on the other hand, import {v4 as uuid} from 'uuid';
works as expected
(tested on uuid v3.3.2
)
感谢您的回答。
我收到以下错误:
MongooseError: document must have an _id before saving
当我尝试使用 API 使用 uuid
创建一个对象(Campagne)时:
import uuidv4 from 'uuid/v4';
当我使用 :
时有效const uuidv4 = require('uuid/v4');
我的 Campagne 对象使用 uuid
.
这是我的对象架构的完整代码:
import * as mongoose from 'mongoose';
import uuidv4 from 'uuid/v4';
export const CampagneSchema = new mongoose.Schema({
_id: { type: String, default: uuidv4 },
dateDebut: Date,
dateFin: Date,
reduction: Number,
});
TSLint 告诉我使用 import
而不是 require()
并在我的 IDE 中将其作为错误下划线,但它肯定无法正常工作,如上所示。
有人能解释一下为什么会这样吗?
有关信息,我使用带有 Typescript 的 NestJS node.js 框架。
澄清:
我想知道为什么 import
适用于 mongoose
但不适用于 uuid
(require
适用于 uuid
)
删除_id: { type: String, default: uuidv4 },
Mongoose 会自动生成_id
并使用const ddd = require(...)
我认为 ES6 模块在 Node 项目中不能正常工作
我在 Github node-uuid
上找到了问题的答案。
以下代码有效:
import {v4 as uuid} from 'uuid';
https://github.com/kelektiv/node-uuid/issues/245
import uuid from 'uuid/v4';
syntax does not work, at least in aTypescript v3
project running onNode v10.9.0
(without webpack nor babel, onlyts-node
to compile/run Typescript)I get the following error:
TypeError: v4_1.uuid is not a function
on the other hand,
import {v4 as uuid} from 'uuid';
works as expected(tested on
uuid v3.3.2
)
感谢您的回答。