MongooseError with uuid when using import instead of require()

Phie

I get the following error :

MongooseError: document must have an _id before saving

When I try to create an Object (Campagne) with uuid with my API using :

import uuidv4 from 'uuid/v4';

It works when I use :

const uuidv4 = require('uuid/v4');

My Campagne object is created correctly with its uuid.

Here is the full code of my object's Schema :

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 tell me to use import instead of require() and underline it as an error in my IDE but it's definitely not working as shown above.

Can someone explain me why is this happening please ?

For information, I use the NestJS node.js framework with Typescript.

To clarify :

I want to know why import is working for mongoose but not for uuid (require is working for uuid)

Phie

I found the answer with an issue on Github node-uuid. The following code is working :

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)

Thanks for the answers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Module not found when using import instead of require

import many items using "require" keyword using instead of "import"

Rails: drawbacks of using Sass's @import instead of *=require

Call single mocha test using `import` statement instead of `require`

Add a module in in ES6 using import instead of require

Mongoose is undefined when using import syntax and not when using require

WebStorm Auto import - "require" instead of "import from"

Unexpected identifier when using import abc = require('node-module')

instead of using import { a, b } from 'some-lib', what is the equivalent using require?

postcss.config with import instead of require

How to use import instead of require with node js?

Make typescript to output "require" instead of "import"

How to create an express Application with import instead of require

Azure Functions use import instead of require

TransformerException when using xsl:import instead of xsl:include

When to use UUID instead of millisecond timestamp in Cassandra?

When to use require and when to use import in Aurelia?

Require, Import, Require Import

In Typescript, how can I make it understand the types when I import by using require()?

Unable to import (require) css, less, sass file on react component when using express server

Can I import *.d.ts instead of require it?

Gulp-sass 5.0 how to use compiler with import() instead of require()?

What is the advantage of using a require statement instead of require_once?

Directly using require() in Express instead of placing in a variable

Using require() and import for private npm modules failing

Trouble using require vs import React Native

Javascript, using require() and import in the same file

Can't import pictures in Reactjs using require()

Unable to load local files using import/require