Node JS self._callback.apply is not a function error - javascript

import axios from 'axios';
import * as jwt from 'jsonwebtoken';
import { Action } from 'routing-controllers';
import { Connection } from 'typeorm';
import { env } from '../env';
export function authorizationChecker(connection: Connection):
(action: Action, roles: any[]) => Promise<boolean> | boolean {
let validationKey: any = axios.get(env.auth.jwksUri).then(response => {
validationKey = response.data.keys[0].value;
}).catch();
return async function innerAuthorizationChecker(
action: Action,
roles: string[]
): Promise<boolean> {
// here you can use request/response objects from action
// also if decorator defines roles it needs to access the action
// you can use them to provide granular access check
// checker must return either boolean (true or false)
// either promise that resolves a boolean value
try {
const token = (action.request.headers.token ||
action.request.headers.authorization).replace('Bearer ', '');
await jwt.verify(token, validationKey);
return true;
} catch (e) {
return false;
}
};
}
This is my error:
error: [app] Application is crashed: TypeError: self._callback.apply is not a
function
(node:512) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): TypeError: Cannot read property '0' of undefined
(node:512) [DEP0018] DeprecationWarning: Unhandled promise rejections are
deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with a non-zero exit code.
I'm trying to get the validation key set from a specific URL from a service, but somehow (without calling the function) it says that the response data is undefined. On top of that, I'm having this error: self._callback.apply is not a function.
Has someone dealt with such a callback error before? I couldn't find anything on stackoverflow relating to my problem.

Related

Not getting a 404 for Patch for user that does not exist

I am expecting that when I send a PATCH/update to a user that does not exist, I should get back a 404, but instead I am getting back a 200.
### Update a user
PATCH http://localhost:3000/auth/2345678
Content-Type: application/json
{
"password": "letmein"
}
HTTP/1.1 200 OK
X-Powered-By: Express
Date: Thu, 09 Sep 2021 19:41:13 GMT
Connection: close
Content-Length: 0
In the console I do get back:
(node:36780) UnhandledPromiseRejectionWarning: NotFoundException: user
not found
at UsersService.update (/Users/luiscortes/Projects/car-value/src/users/users.service.ts:27:13)
(Use node --trace-warnings ... to show where the warning was
created) (node:36780) UnhandledPromiseRejectionWarning: Unhandled
promise rejection. This error originated either by throwing inside of
an async function without a catch block, or by rejecting a promise
which was not handled with .catch(). To terminate the node process on
unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict (see
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 1) (node:36780) [DEP0018] DeprecationWarning: Unhandled
promise rejections are deprecated. In the future, promise rejections
that are not handled will terminate the Node.js process with a
non-zero exit code. (node:36780) UnhandledPromiseRejectionWarning:
NotFoundException: user not found
at UsersService.update (/Users/luiscortes/Projects/car-value/src/users/users.service.ts:27:13)
(node:36780) UnhandledPromiseRejectionWarning: Unhandled promise
rejection. This error originated either by throwing inside of an async
function without a catch block, or by rejecting a promise which was
not handled with .catch(). To terminate the node process on unhandled
promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 2)
but why don't I get a 404 in my REST Client?
This is my users.controller.ts file:
import {
Body,
Controller,
Post,
Get,
Patch,
Param,
Query,
Delete,
NotFoundException,
} from '#nestjs/common';
import { CreateUserDto } from './dtos/create-user.dto';
import { UpdateUserDto } from './dtos/update-user.dto';
import { UsersService } from './users.service';
#Controller('auth')
export class UsersController {
constructor(private usersService: UsersService) {}
#Post('/signup')
createUser(#Body() body: CreateUserDto) {
this.usersService.create(body.email, body.password);
}
#Get('/:id')
async findUser(#Param('id') id: string) {
const user = await this.usersService.findOne(parseInt(id));
if (!user) {
throw new NotFoundException('user not foud');
}
return user;
}
#Get()
findAllUsers(#Query('email') email: string) {
return this.usersService.find(email);
}
#Delete('/:id')
removeUser(#Param('id') id: string) {
return this.usersService.remove(parseInt(id));
}
#Patch('/:id')
updateUser(#Param('id') id: string, #Body() body: UpdateUserDto) {
this.usersService.update(parseInt(id), body);
}
}
This is my users.service.ts file:
import { Injectable, NotFoundException } from '#nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '#nestjs/typeorm';
import { User } from './user.entity';
#Injectable()
export class UsersService {
constructor(#InjectRepository(User) private repo: Repository<User>) {}
create(email: string, password: string) {
const user = this.repo.create({ email, password });
return this.repo.save(user);
}
findOne(id: number) {
return this.repo.findOne(id);
}
find(email: string) {
return this.repo.find({ email });
}
async update(id: number, attrs: Partial<User>) {
const user = await this.findOne(id);
if (!user) {
throw new NotFoundException('user not found');
}
Object.assign(user, attrs);
return this.repo.save(user);
}
async remove(id: number) {
const user = await this.findOne(id);
if (!user) {
throw new NotFoundException('user not found');
}
return this.repo.remove(user);
}
}
You don't return the service call in your path request, so Nest doesn't know to wait for it. The service code then runs in the background after the response has been sent, and causes this unhandledPromiseRejection to show up. Change your patch method to this
#Patch('/:id')
updateUser(#Param('id') id: string, #Body() body: UpdateUserDto) {
return this.usersService.update(parseInt(id), body);
}

Unhandled promise rejection using NestJS

When I execute the following code I get "201 Created" response from the server but actually data is not inserted in the server.
I am using nestJS with TypeORM and Postgres for my application.
import { Repository, EntityRepository } from "typeorm";
import { User } from './user.entity';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
import { ConflictException, InternalServerErrorException } from "#nestjs/common";
#EntityRepository(User)
export class UserRepository extends Repository<User>{
async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void>{
const {username, password} = authCredentialsDto;
const user = new User();
user.username = username;
user.password = password;
try{
await user.save();
} catch (error) {
if (error.code === '23505'){ // error code for duplicate value in a col of the server
throw new ConflictException('Username already exists');
} else {
throw new InternalServerErrorException();
}
}
}
}
And I get following response in the VS Code terminal, instead of getting "201 Crated" from the server:
(node:14691) UnhandledPromiseRejectionWarning: Error: Username already exists
at UserRepository.signUp (/home/rajib/practicing coding/nestJS-projects/nestjs-task-management/dist/auth/user.repository.js:24:23)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14691) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14691) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
And the controller modules code is following:
import { Controller, Post, Body, ValidationPipe } from '#nestjs/common';
import { AuthService } from './auth.service';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
#Controller('auth')
export class AuthController {
constructor( private authService: AuthService){}
#Post('/signup')
signUp(#Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto): Promise<void>{
return this.authService.signUp(authCredentialsDto);
}
}
Good day.
I think the issue you have here is not awaiting the method call to the authService.signup() method in your AuthController...
So, the correction should be:
In the AuthService::signup() method:
return this.userRepository.signup(authCredentialsDto);
In the AuthController::signup() method:
return this.authService.signup(authCredentialsDto);

Setting "params" for GET requests using RESTDataSource

Problem: I am trying to make a GET request using RESTDataSource's get method, but I'm receiving a ERR_INVALID_URL error.
My code:
async getMediaIDs() {
let response;
try {
response = await this.get(`${process.env.INSTA_ID}/media`, {
access_token: `${process.env.PAGE_ACCESS_TOKEN}`,
});
} catch(err) {
throw new Error(err);
}
return response.data.data;
}
Expected: The request is successful, with the full url being:
https://graph.facebook.com/{insta_id}/media?access_token={access_token}
Actual: I receive this error:
2019-11-15T10:03:42.974335+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Error: TypeError [ERR_INVALID_URL]: Invalid URL: 17841402041188678/media
2019-11-15T10:03:42.974379+00:00 app[web.1]: at InstagramAPI.getMediaIDs (/app/src/data/instagram.js:17:13)
2019-11-15T10:03:42.974382+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:93:5)
2019-11-15T10:03:42.974395+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
2019-11-15T10:03:42.974467+00:00 app[web.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I suspect that the error is in
this.get(`${process.env.INSTA_ID}/media`, {
access_token: `${process.env.PAGE_ACCESS_TOKEN}`,
});
because I am not setting the params parameter correctly (...I think. I don't know.)
While searching for a solution, I found the RESTDataSource file, which leads to this:
protected async get<TResult = any>(
path: string,
params?: URLSearchParamsInit, // <----- (What I'm trying to set)
init?: RequestInit,
): Promise<TResult> {
return this.fetch<TResult>(
Object.assign({ method: 'GET', path, params }, init),
);
}
Following URLSearchParamsInit leads to this:
export type URLSearchParamsInit =
| URLSearchParams
| string
| { [key: string]: Object | Object[] | undefined }
| Iterable<[string, Object]>
| Array<[string, Object]>;
I'm not too familiar with TypeScript, but I'm guessing that those are ways to define params?.
Anyways, my question is how do I set the params parameter for RESTDataSource's get method?
side note for the Apollo devs: An API page for RESTDataSource would be brilliant! I would be willing to help document it, as proper documentation currently isn't available.
Based on the error you're seeing, it doesn't look like you're setting a baseURL. Without the baseURL set, you end up with 17841402041188678/media as the complete URL, which is not a valid URL. You can set the baseURL inside your RESTDataSource's constructor:
class InstagramAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://graph.facebook.com/';
}
// ...
}

Hapi.js UnhandledPromiseRejectionWarning: Error: reply interface called twice?

when I running my project, I get the error:
(node:5795) UnhandledPromiseRejectionWarning: Error: reply interface called twice
at Object.exports.assert (/Users/labikemmy/Downloads/React-Native-FriendChat/api/node_modules/hoek/lib/index.js:736:11)
at Function.internals.response (/Users/labikemmy/Downloads/React-Native-FriendChat/api/node_modules/hapi/lib/reply.js:164:10)
at bound (domain.js:301:14)
at Function.runBound (domain.js:314:12)
at reply (/Users/labikemmy/Downloads/React-Native-FriendChat/api/node_modules/hapi/lib/reply.js:72:22)
at bound (domain.js:301:14)
at runBound (domain.js:314:12)
at result.then (/Users/labikemmy/Downloads/React-Native-FriendChat/api/node_modules/hapi/lib/handler.js:105:36)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
(node:5795) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5795) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
null
I don't know it is bug or my code error? I'm screen hapi.js issues, and someone said the error is bug, another said 'reply() times is limit in same request'? if it is limited, how to change the code at below?
```
export default async function (request, reply) {
if (request.auth.credentials.email !== request.payload.email) {
await User.findOne({ email: request.auth.credentials.email }).then(
(user) => {
if (user) {
User.findOne({ email: request.payload.email }).then(
(friend) => {
if (friend) {
const stringId = `${friend._id}`;
const friendExists = user.friends.filter(f => `${f}` === stringId).length > 0;
if (!friendExists) {
user.friends.push(friend);
user.save();
reply({ friend: { fullName: friend.fullName, _id: friend._id } });
} else {
reply(Boom.conflict('You have added already this friend'));
}
} else {
reply(Boom.notFound(`Friend ${request.payload.email} doesn't exist`));
}
},
);
} else {
reply(Boom.notFound('Cannot find user'));
}
},
);
} else {
reply(Boom.conflict('Cannot add yourself as a friend'));
}
}
Hapi#16.4.1
Do you have any other plugins or lifecycle hooks like onPreHandler or something? Maybe there is some point your code that throws this error because you (or your code somehow) are calling reply interface before your actual response.
Also, I refactored your code. You are already utilizing JavaScript async interface, so you don't need to put "then" calls to your promises.
Try this and watch that what will come out:
export default async function (request, reply) {
if (request.auth.credentials.email === request.payload.email) {
return reply(Boom.conflict('Cannot add yourself as a friend'))
}
// I belive this is mongoose model
const user = await User.findOne({email: request.auth.credentials.email}).exec();
if (!user) {
return reply(Boom.notFound('Cannot find user'));
}
const friend = await User.findOne({email: request.payload.email}).exec();
if (!friend) {
return reply(Boom.notFound(`Friend ${request.payload.email} doesn't exist`));
}
const stringId = `${friend._id}`;
const friendExists = user.friends.filter(f => `${f}` === stringId).length > 0;
if (!friendExists) {
// hmmm shouldn't it be friend._id? user.friends.push(friend._id.toString());
user.friends.push(friend);
// better use this statement
// ref: http://mongoosejs.com/docs/api.html#document_Document-markModified
user.markModified('friends');
await user.save();
return reply({friend: {fullName: friend.fullName, _id: friend._id}});
} else {
return reply(Boom.conflict('You have added already this friend'));
}
}

Unhandled promise rejection from promise called within Try

I have a promise function called findProperty which in this case rejects like this:
reject('ERR: Unknown propertyID or inactive property in call of newBooking');
And this is my handler that calls findProperty:
async function main() {
var res = "";
try {
findProperty();
res = await findUser(userEmail);
res = await findUser(agentEmail);
res = await getUsers();
}
catch(err) {
console.error(err);
console.log(" newBooking: " + err);
callback( { error:true, err } );
}
}
main();
This causes the following error:
(node:10276) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ERR: Unknown propertyID or inactive property in call of newBooking
(node:10276) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I dont get this, I got my catch(err) shouldn't that be enough?
Just tried something, this works fine:
resolve('ERR: Unknown propertyID or inactive property in call of newBooking');
But this gives the error:
reject('ERR: Unknown propertyID or inactive property in call of newBooking');
If findProperty returns a promise, you need to await it in order to trigger the failure within the context of the async function; otherwise the rejection disappears into outer space.
To "fire and forget" without waiting, yet catch failures with your try/catch:
findProperty().catch(e => { throw e; });

Categories