2

I'm using node.js with [email protected], [email protected], [email protected] and [email protected]

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 Answer 1

2

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});
        }
    }
)
Sign up to request clarification or add additional context in comments.

7 Comments

Also keep in mind the typings are for Mongoose v3 but you're using Mongoose v4.
Thanks for your response. This gives another error: TS2346: Supplied parameters do not match any signature of call target. On the line: res.jsonp();
@HermanFransen You would pass whatever response data you want to return as a parameter to the res.jsonp call. e.g. res.jsonp({success: true}).
If I keep it the way it was, then I get an object returned: {ok: 1, n: 1}. So I guess that the res.jsonp(document); does it ok. So nothing is wrong with the coding I guess. And maybe @Harangue is right that the typings I am using are for mongoose v3 while I am using v4.
@HermanFransen It's basically a problem of undocumented behavior. The typings file is in agreement with the Mongoose docs, but there's an undocumented second parameter passed to the callback from the underlying driver.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.