right now i am throwing error if "dateofBirth" is not found that just sent empty object back to client , how can i pass error object back to client instead of empty object. basically want to send back to process catch
main.ts
export class GetSpecialtyQuestionsController extends Controller {
public static async process(#Request() request: ExpressRequest,
response: ExpressResponse): Promise < any > {
try {
const instance = new GetSpecialtyQuestionsController();
const data = await instance.execute(request);
response.status(200);
response.send(data);
} catch (err) {
response.status(200);
response.send(err.message);
}
}
// private _request: IRequestURL[] = [];
constructor() {
super();
}
private async execute(#Request() request: ExpressRequest): Promise < any > {
// const specialtyMembers = this.getSpecialtyMemberInfoFakeObject();
const specialtyMembers = await new SpecialtyCacheUtility().getSpecialtyMemberInfoCache(
request.body.getSpecialtyQuestionsRequest.header.serviceContext.tokenID);
if (!specialtyMembers) {
return this.errorHandler(request);
}
let proxyMember: ISpecialtyInfoObj = {}
as ISpecialtyInfoObj;
for (const member of specialtyMembers) {
if (member.specialtyIdEnc === request.body.getSpecialtyQuestionsRequest.details.specialtyIdEnc) {
proxyMember = member;
if (!member.dateOfBirth) {
throw new Error('no patient info for given HBS ID');
}
break;
}
}
}
Create your custom exception object to pass data while throw error
class CustomError extends Error {
constructor(foo = 'bar', ...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CustomError);
}
// Custom debugging information
this.foo = foo;
this.date = new Date();
}
}
try {
throw new CustomError('baz', 'bazMessage');
} catch(e){
console.log(e.foo); //baz
console.log(e.message); //bazMessage
console.log(e.stack); //stacktrace
}
For reference read more from official mdn documentation - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
Related
I'm sending a message to an SQS queue as follows in typescript:
const queueName = 'some_queue_name';
const foo = 'foo';
const bar = 'bar';
const sqs = new AWS.SQS();
const SendMessageRequest: AWS.SQS.SendMessageRequest = {
MessageBody: JSON.stringify({ foo, bar }),
QueueUrl: queueName,
};
try {
const request = sqs.sendMessage(SendMessageRequest, function (err, data) {
if (err) {
console.log('Error', err);
}
});
const smgResponse: SendMessageResult = await request.promise();
} catch (err) {
console.log(err);
expect(err).toBeUndefined();
}
At runtime, the SendMessageRequest shows a $response property but this isn't visible in the object definition. What is the best way to access this property at runtime?
Use a type assertion:
const smgResponse = await request.promise() as SendMessageResult & {$response: unknown}
Replace unknown with whatever type $response is at runtime.
If you are doing a lot of assertions you could create a new type for this:
interface SendMessageResultWithResponse extends {
$response: unknown
}
// alternatively type SendMessageResultWithResponse = SendMessageResult & {$response: unknown}
const smgResponse = await request.promise() as SendMessageResultWithResponse
The original error say's: Cannot destructure property 'firstime' of 'undefined' or 'null'.
I am developing web-base desktop application for Windows pc using node.js and Electron.
I am trying to persist some data in user data directory, I found the idea and using the same approach in this link.
Writing and fetching data works fine, however the error occurred at the first time of fetching the data.
here is the code for UserPreferences class
const electron = require('electron');
const path = require('path');
const fs = require('fs');
class UserPreferences {
constructor(opts) {
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
this.path = path.join(userDataPath, opts.configName + '.json');
this.data = parseDataFile(this.path, opts.defaults);
console.log(userDataPath);
}
get(key) {
return this.data[key];
}
set(key, val) {
this.data[key] = val;
fs.writeFileSync(this.path, JSON.stringify(this.data));
}
}
function parseDataFile(filePath, defaults) {
try {
return JSON.parse(fs.readFileSync(filePath));
} catch (error) {
return defaults;
}
}
module.exports = UserPreferences;
and here's the function for using the UserPreferences class
function isFirstTime() {
try{
const userAccount = new UserPreferences({
configName: 'fipes-user-preferences', // We'll call our data file 'user-preferences'
defaults: {
user: { firstime: true, accountid: 0, profileid: '' }
}
});
var { firstime, accountid, profileid } = userAccount.get('user');
if (firstime === true) { //check if firstime of running application
//do something
} else {
//do something
}
}catch(err){
console.log(err.message);
}
}
the error occurred on the line where I am checking weather the firstime is true or false.
First of all do not declare a object like var { firstTime, .. } like this. if you do this firstTime will be a property of an anonymous object. That you can never access elsewhere. Check what is the output of userAccount.get('user') function, output contain some object like { firstime: true, accountid: "test", profileid: "test" } then try this. Hope this helps you.
var result=userAccount.get('user');
if(result.firstTime===true){
//your code
}
Here is a version of UserPreferences which will be more natural to use as you write your code. You can create it like you see in isFirstTime.
console.debug(userPreferences[accountId]);
userPreferences[accountId] = 1;
This is preferred because there is no reason for a developer not to treat UserPreferences as an object. Another good idea would be separating the writing to the file into a separate flush method, in case you are updating preferences often.
const electron = require("electron");
const fs = require("fs");
const path = require("path");
class UserPreferences {
constructor(defaultPrefs, pathToPrefs) {
const app = electron.app || electron.remote.app;
this.pathToPrefs = path.join(app.getPath("userData"), pathToPrefs + ".json");
try {
this.store = require(this.pathToPrefs);
}
catch (error) {
this.store = defaultPrefs;
}
return new Proxy(this, {
get(target, property) {
return target.store[property];
},
set(target, property, value) {
target.store[property] = value;
fs.writeFileSync(target.pathToPrefs, JSON.stringify(target.store));
}
});
}
}
module.exports = UserPreferences;
Here is a pure version of isFirstTime, that should do what you want, while maintaining a more robust method of checking for isFirstTime. The check can also be changed so check whether lastSignIn is equal to createdAt (with appropriate defaults, of course).
function isFirstTime() {
const account = new UserPreferences({
user: {
accountId: 0,
createdAt: new Date(),
lastSignIn: null,
profileId: ""
}
}, "fipes-user-preferences");
const {lastSignIn} = account;
return lastSignIn === null;
}
I am trying to insert data from an API call to the database. The API call is done from the global services. When I tried to do it within the constructor method it is working. But if I try it from an other outside method from service I am getting an error “ERROR TypeError: Cannot read property 'setMetadataKey' of undefined(…)”.
import { Database } from './../providers/database/database';
import {Injectable} from '#angular/core';
#Injectable()
export class mAppService{
showValue: string;
public database;
userLoggedIn: boolean = false;
constructor(database: Database) {
//If i call the database method from here its working**
}
getConnect(ipaddress,portnumber){
try {
var t = ipaddress;
var p = portnumber;
client = new linear.client({transports: [
{type: 'websocket',
host: t, port: p}
]});
client.connect();
client.transport.onopen=this.onopen;
client.transport.onclose=this.onclose;
client.onconnect = client.ondisconnect = this.getStatus;
client.onnotify = this.recvNotify;
let stream$ = new Observable(observer => {
let interval = setInterval(() =>{
observer.next(client.transport.state);
/*observer.error(client.transport.onclose);
observer.complete();*/
},1000);
return() =>{
clearInterval(interval);
}
}).share();
return stream$;
} catch (error) {
console.log("catch.."+error);
}
}
recvNotify(m) {
var method = m.name;
var value = m.data;
var buttonType = "EFT_PAN";
this.database.setMetadataKey(value,buttonType).then((result) => {
console.log("Successfully saved the setMetadataKey.")
}, (error) => {
alert("ERROR: "+ error);
});
}
}
Can anyone help me to solve this please
I am facing a difficulty in obtaining the property value from a javascript class object.
The following is the class files and how I try to get its property value:
1: I defined a class named School in classes.js
export class School{
constructor()
{
this.schools = []; //to store schools returned from backend
}
//used to get all schools
getall()
{
axios.get('/api/schools')
.then(response =>{
this.schools = response.data;
console.error('School length: ' + this.schools.length);
this.ok = true;
//add origin data property
for(let i = 0; i < this.schools.length; i++)
{
this.schools[i]['orgValue'] = new tagChange();
}
})
.
catch(error => {
this.ok = false;
this.error = error.status;
this.schools = [];
});
return this.schools;
} //getall
}
2: I import this School class to another js file
//import School class
import {tagChange, School} from './classes.js';
export default {
components: {
modalAddSchool,
},
data() {
return {
schools: [], // my schools in
mySchoolObj: new School(), //create a School object
}
},
methods: {
getAllSchools()
{
//after this call, mySchoolObj's schools property indeed contain 8 elements in array format,
//However, this.schools can not be assigned successfully, so unexplainable
this.schools = this.mySchoolObj.getall();
//print zero element
console.log(this.mySchoolObj.schools);
//print zero element
console.log(this.schools)
}
},
3: after call getAllSchools() method, the mySchoolObj.schools indeed contain 8 school elements but this.schools cannot be assigned successfully, neither the following two console.log call can only print zero length
4: I really want to know how to return all the mySchoolObj.schools to this.schools, and how to get/visit its other property value?
axios.get is asynchronous, it means that when you return this.schools;, the ajax call is not finished yet so you return an empty array [].
More informations here:
Synchronous and asynchronous requests
You can return the promise given by axios or use a callback, like that:
//used to get all schools
getall(callback) { // take a callback function
axios.get('/api/schools')
.then(response =>{
this.schools = response.data;
console.error('School length: ' + this.schools.length);
this.ok = true;
//add origin data property
for (let i = 0; i < this.schools.length; i++) {
this.schools[i]['orgValue'] = new tagChange();
}
if (typeof callback === 'function') {
callback(this.schools, null); // call the callback with data and null (null because there is no error)
}
})
.catch(error => {
this.ok = false;
this.error = error.status;
this.schools = [];
if (typeof callback === 'function') {
callback(null, error.status); // call the callback with null data and the error status
}
});
return this.schools;
}
Then you can use your method like that:
methods: {
getAllSchools() {
this.mySchoolObj.getall((data, error) => {
if (error) {
return console.log(error);
}
this.schools = data;
console.log(this.schools);
});
}
},
(this code isn't tested, it may contain bugs)
My code that I have looks like this:
class UserService implements IUserService {
data: IUserServiceData = {
expirationDate: null,
isAuthenticated: false,
};
static $inject = [];
constructor () {}
isAuthenticated = () => {
if (this.data.isAuthenticated && !this.isAuthenticationExpired(this.data.expirationDate)) {
return true;
} else {
try {
this.retrieveSavedData();
} catch (e) {
return false;
// throw new NoAuthenticationException('Authentication not found');
}
return true;
}
};
// Original Javascript code here
//function AuthenticationRetrievalException(message) {
// this.name = 'AuthenticationRetrieval';
// this.message = message;
//}
AuthenticationRetrievalException = (message) => {
this.name = 'AuthenticationRetrieval';
this.message = message;
}
retrieveSavedData = () => {
var savedData = this.utilityService.userCache.get('data');
if (typeof savedData === 'undefined') {
throw new AuthenticationRetrievalException('No authentication data exists');
} else if (isAuthenticationExpired(savedData.expirationDate)) {
throw new AuthenticationExpiredException('Authentication token has already expired');
} else {
this.data = savedData;
this.setHttpAuthHeader();
}
}
}
What should I do with the this. references that were in my JavaScript source before I started to try and convert it?
I don't know how to code this part in Typescript:
AuthenticationRetrievalException = (message) => {
this.name = 'AuthenticationRetrieval';
this.message = message;
}
If I understand you well and you want to trow an error with custom message, you can use class Error like this:
throw new Error("Authentication not found");
But I am not sure if this is what you want to do.
I highly recommend you not creating your own Error classes in JavaScript(or TypeScript) and just use Error (ref : How do I create a custom Error in JavaScript?)
But here is how you can do it in TypeScript. At the root level of your file (not inside your class):
function AuthenticationRetrievalError (message) {
this.name = "AuthenticationRetrievalError";
this.message = (message || "");
}
AuthenticationRetrievalError.prototype = Error.prototype;
Then then from inside your class :
throw new AuthenticationRetrievalError('foo');