2
votes

I'm using node.js with typescript@1.7.5, express@4.13.3, mongoose@4.3.7 and mongodb@3.2.0

I get compile error:

TS2345: Argument of type '(error: any, document: any) => void' is not assignable to parameter of type '(err: any) => void'.

at the line: (error, document) => {

Everything works fine at runtime even with this compile error.
How can I solve this error?

import express = require("express");
import bodyParser = require('body-parser');
import mongoose = require("mongoose");

import contactListModel = require("./contactlistSchema");
var contact = contactListModel.contact;

export function removeOne (req: express.Request, res: express.Response) {
    var id = req.params.id;
    console.log("delete one contact in database with id: " + id);
    contact.remove(
        {_id: new mongoose.Types.ObjectId(id)},
        (error, document) => {
            if(error){
                console.log(error);
                res.sendStatus(500);
            } else {
                console.log(document)
                res.jsonp(document);
            }
        }
    )
}

contactlistSchema.ts

import mongoose = require("mongoose");

export var contactlistSchema = new mongoose.Schema({
    id: String,
    name: String,
    email: String,
    number: String,
    type: String

});

export interface IContactList extends mongoose.Document{
    id: string;
    name: string;
    email: string;
    number: string;
    type: string

}

export var contact = mongoose.model<IContactList>("contact", contactlistSchema);
1

1 Answers

2
votes

You're getting that error because Model.remove doesn't provide the removed doc to the callback.

So the code will still run, but document will be undefined in your callback.

To resolve the error, just remove the document parameter from your callback:

contact.remove(
    {_id: new mongoose.Types.ObjectId(id)},
    (error) => {
        if(error){
            console.log(error);
            res.sendStatus(500);
        } else {
            res.jsonp({success: true});
        }
    }
)