Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[types] Model.create dosnt return array #9817

Closed
hasezoey opened this issue Jan 15, 2021 · 5 comments
Closed

[types] Model.create dosnt return array #9817

hasezoey opened this issue Jan 15, 2021 · 5 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@hasezoey
Copy link
Collaborator

Do you want to request a feature or report a bug?
type-bug

What is the current behavior?
await SomeModel.create([doc1, doc2]) dosnt return an array

If the current behavior is a bug, please provide the steps to reproduce.
code used:

// NodeJS: 14.15.1
// MongoDB: 4.2-bionic (Docker)
// import { getModelForClass, prop } from "@typegoose/typegoose"; // @typegoose/[email protected]
import * as mongoose from "mongoose"; // [email protected]

interface ISomeModel extends mongoose.Document {
  param1: string,
  param2: number,
}

const SomeSchema = new mongoose.Schema({ param1: String, param2: Number });
const SomeModel = mongoose.model<ISomeModel>("SomeModel", SomeSchema);

(async () => {
  await mongoose.connect(`mongodb://localhost:27017/`, { useNewUrlParser: true, dbName: "verifyMASTER", useCreateIndex: true, useUnifiedTopology: true });

  const [...docs] = await SomeModel.create([{ param1: "Hello", param2: 1 }]);
  const doc = await SomeModel.create({});


  await mongoose.disconnect();
})();
{
    "compileOnSave": true,
    "typeAcquisition": {
        "enable": true
    },
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "declarationMap": true,
        "outDir": "lib",
        "moduleResolution": "node",
        "target": "es6",
        "module": "commonjs",
        "newLine": "LF",
        "sourceMap": true,
        "removeComments": true,
        "strict": true,
        "allowUnreachableCode": false,
        "pretty": true,
        "declaration": true,
        "lib": [
            "esnext"
        ]
    },
    "include": [
        "src/**/*"
    ]
}

What is the expected behavior?
that await SomeModel.create([doc1,doc2]) returns an array

What are the versions of Node.js, Mongoose and MongoDB you are using?
mongoose 5.11.12
typescript 4.1.3

would have provided an pr, but didnt find an fix yet

@imduchy
Copy link
Contributor

imduchy commented Jan 16, 2021

The problem seems to be here

create<Z = T | DocumentDefinition<T>>(docs: Array<Z>, options?: SaveOptions): Promise<Array<T>>;

/** Creates a new document or documents */
create<Z = T | DocumentDefinition<T>>(doc: Z): Promise<T>;
create<Z = T | DocumentDefinition<T>>(docs: Array<Z>, options?: SaveOptions): Promise<Array<T>>;

If you don't specify an options parameter (although it's optional...) it will assume that the return value is T.

Try to do this. It should help.

await SomeModel.create([{ param1: "Hello", param2: 1 }], {});

I'm not sure how to fix that though - my guess would be that it should work, but it doesn't. I'm not as experienced with type declarations.

@hasezoey
Copy link
Collaborator Author

thanks @Duchynko, do you know why this line is ignored then?

create<Z = T | DocumentDefinition<T>>(...docs: Array<Z>): Promise<T>;

Note: i already manually tried to set the return of this line to Promise<Array<T>> (which is i think the proper one for this line), but it didnt change what typescript will use
-> and i also tried switching line 634 and 635 - which didnt change the outcome (i also tried just commenting out the line 634, which also didnt change anything)

@imduchy
Copy link
Contributor

imduchy commented Jan 17, 2021

@hasezoey I'm not sure, hah. I also tried to play around with this but didn't figure it out.

create<Z = T | DocumentDefinition<T>>(...docs: Array<Z>): Promise<T>;

@vkarpov15 shouldn't the above overload return an array? I can create a PR if so...

create<Z = T | DocumentDefinition<T>>(...docs: Array<Z>): Promise<Array<T>>;

@vkarpov15
Copy link
Collaborator

@Duchynko you're right, it should. Passing a spread of docs means you get a spread in the callback, but you do get an array of docs if you use a promise. Feel free to put in a PR 👍

@hasezoey
Copy link
Collaborator Author

hasezoey commented Feb 5, 2021

actual issue should get fixed by #9890 (didnt test it yet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants