Search code examples
node.jstypescriptmongoosemongoose-schema

Keep getting the same error when building a simple schema/database file combination


Just building a simple database/schema files and keep getting this error when I tsc:

node_modules/mongodb/mongodb.d.ts:3314:5 - error TS2416: Property 'end' in type 'GridFSBucketWriteStream' is not assignable to the same property in base 
type 'WritableStream'.
  Type '{ (): void; (chunk: Buffer): void; (callback: Callback<void | GridFSFile>): void; (chunk: Buffer, callback: Callback<void | GridFSFile>): void; (chunk: Buffer, encoding: BufferEncoding): void; (chunk: Buffer, encoding: BufferEncoding, callback: Callback<...>): void; }' is not assignable to type '{ (cb?: () => void): this; (data: string | Uint8Array, cb?: () => void): this; (str: string, encoding?: BufferEncoding, cb?: () => void): this; }'.       

3314     end(chunk: Buffer, encoding: BufferEncoding | undefined, callback: Callback<GridFSFile | void>): void;

I've searched everywhere but can't find a solution. This are my two files:

\\database.ts
import * as mongoose from 'mongoose'
var companyInfo =require('./companiesSchema/Company');

mongoose.connect("mongodb://localhost/FinancialReports")

saveToDB();

async function saveToDB() {
    const company = await new companyInfo({companyName: "AMZN", companyStatments: "sdfsdgsdgsdgs"}) ;
    await company.save()
    console.log("Company Saved")
}

and

\\companiesSchema.ts
import * as mongoose from 'mongoose'

const companySchema = new mongoose.Schema({
   companyName: String,
   companyStatments: String
})

module.exports = mongoose.model('Company', companySchema);

Solution

  • It seems it is an error which happens only in @types/node v^17.0.6. Just change to a previous version (^16.11.7 worked for me) until the bug is fixed. More info here: https://github.com/mongodb/node-mongodb-native/pull/3088