1

Yo, I have this peice of code in my API and it gives me an error I don't understand.

Code :

server.get('/deleteall', async (request, reply) => {
  var json = JSON.stringify(deleteall);
  fs.writeFile('todos.json', json, 'utf8', callback);
  return 'File overwritten.'
})

Error :

Argument of type '(arg0: string, json: string, arg2: string, callback: any) => void' is not assignable to parameter of type 'NoParamCallback'.

The error happens on build, I don't understand it at all, keep in mind, this is my first TS experience.

0

1 Answer 1

3

The callback argument for fs.write() is defined like this:

export type NoParamCallback = (err: NodeJS.ErrnoException | null) => void;

Whatever you have defined as callback does not match that type definition. It should be something like:

function callback(err) {
  console.log(err);
}

Or specified inline:

fs.writeFile('todos.json', json, 'utf8', (err) => console.log(err));

As a more general comment: I suggest you migrate to the promises API to escape callback hell.

Sign up to request clarification or add additional context in comments.

2 Comments

I followed this guide, here's my full code
Right. As I said: that callback method you define at the end does not match the expected signature.

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.