logo
down
shadow

Express promise throw error


Express promise throw error

By : Denis Saliasi
Date : December 01 2020, 05:00 PM
may help you . I am using Express with denodeify module. I`m getting error when i try to render email template asynchronously. , I found the solution, we have to bind "this" variable like this
code :
 return denodeify(res.render.bind(res))(path.resolve('modules/users/server/templates/verify-email'), {
      name: user.displayName,
      appName: config.app.title,
      mail: user.email,
      url: 'http://' + req.headers.host + '/api/auth/verify/' + user.verificationToken + "/" + user.email
 });
  var app = this.req.app;
  var done = callback;
  var opts = options || {};
  var req = this.req;
  var self = this;
denodeify(res.render)(path, options)


Share : facebook icon twitter icon
Node.js (with socket.io and express) throw "events.js:71 throw arguments[1]; // Unhandled 'error' event"

Node.js (with socket.io and express) throw "events.js:71 throw arguments[1]; // Unhandled 'error' event"


By : David Nacif Solis
Date : March 29 2020, 07:55 AM
With these it helps If you are using node.js version 0.8.20, then this bug was observed by many including myself.
https://github.com/LearnBoost/socket.io/issues/1160
How can I convert a rejected promise to an exception and throw it from an Express route handler?

How can I convert a rejected promise to an exception and throw it from an Express route handler?


By : Matt Barker
Date : March 29 2020, 07:55 AM
To fix the issue you can do This does not answer your question directly, but rather shows another way to achieve your goal.
Every middleware handler in express has the signature (request, response, next). Currently your index function does not have next defined.
code :
dao.findById(id)
   // Handle success
    .then(function (data) {
        res.send(data);
    })
    // Handle failure
    .catch(next);
How do I know which handlers throw error in promise?

How do I know which handlers throw error in promise?


By : Dinmukhamed Baiznano
Date : March 29 2020, 07:55 AM
wish of those help everyone! I had researched demonstrated code by myself.
I hoped everyone can review my answer, it's good or not.
code :
var Promise = require('bluebird');

// You can input any argument in this function to test workflow of promise 
function testPromise(input) {
    let promise = Promise.resolve(input);
    promise
      .then(makeTask('Task1'))
      .then(makeTask('Task2'))
      .then(makeTask('Task3'))
      .catch(makeErrorPredicate('Task1'), taskLog('Task1'))
      .catch(makeErrorPredicate('Task2'), taskLog('Task2'))
      .catch(makeErrorPredicate('Task3'), taskLog('Task3'))
}

// define task handler functions
function makeTask(task) {
    return function task1Handler(input) {
        if (input === task) {
            throw new Error(task)
        }            
        return input
    }
}

// custom error that it checks error message 
function makeErrorPredicate(msg) {
    return function taskError(err) {
        var result = err.message === msg;
        console.log(msg + ': ' + result)
        return result;
    }
}

// hint the error when the error has matched
function taskLog(msg) {
    return function thelog(err) {
        console.log('It\'s ' + msg)
    }  
}
>testPromise('Task1')
Task1: true
It's Task1

>testPromise('Task2')
Task1: false
Task2: true
It's Task2

>testPromise('Task3')
Task1: false
Task2: false
Task3: true
It's Task3
throw error in catch block Q promise not rejecting the promise

throw error in catch block Q promise not rejecting the promise


By : Anand Selvam
Date : March 29 2020, 07:55 AM
help you fix your problem You're getting a multitude of logs because you use a recursive approach, and handle and rethrow the error on every level. Written synchronously, it's exactly the same as
code :
function fetchAll(offset) {
    if (offset > 5) throw new Error("message"); // let's say the inner one throws
    try {
        return fetchAll(offset+1);
    } catch(e) {
        console.error(e.message);
        throw e;
    }
}
fetchAll(0);
function fetchAllRecords(client, item, region, offset=1, savedCount=0, totalCount=0) {
  return processQueue(client, item, offset, region).then(function (result) {
    return associate(result, item, region)
  }).then(function (success) {
    return saveBatch(item, success.allResources, region);
  }).then(function (result) {
    savedCount += result.savedCount;
    totalCount += result.totalCount;
    if (debugmode) {
      console.log(savedCount + '/' + totalCount);
    }
    offset += item.limit;
    return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
  }, function (err) {
// ^^
    if (err == 'done')
      return 'done';

    if (err.message === 'LIMIT_EXCEEDED') {
      offset += item.limit;
      return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
    } else {
      console.log('Miscellenious error in fetchAllRecords, moving to next resource');
      console.log(JSON.stringify(err));
      throw new Error('Misc');
    }
  });
}
function processResource(client, item, debugmode, region) {
  if (item.resource === "Property" && region === "ccmls") {
    var cities = cities.list;
    item.query = item.query + ' ,(City=|' + cities.join() + ')';
  }
  return fetchAllRecords(client, item, region)
  .then(function (result) {
    if (debugmode) {
      console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
    }
    return result;
  }, function (err) {
    console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
    throw err;
  });
}
Really throw an error inside a promise

Really throw an error inside a promise


By : Mohini
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can check the input before you build the return Promise and throw there. That is throwing in myFunc.
code :
function myFunc(input) {
  if (!input)
      throw "invalid input!"
  return new Promise(function(resolve, reject) {
       // do stuff
       resolve(result);
       //if doesn't work
       reject(reason);

  });
}
try{
    myFunc();
}
catch(e)
{
    console.log("It does throw and doesn't give you the promise", e)
}
p1.then((result) => {
        // deal with result
    })
    .catch((reason)=>{
        // you get the error you rejected in the promise
    });
Related Posts Related Posts :
  • CRM 2013 Limit the date field
  • fancybox - changing close button color
  • Ember-notify giving error can't read property "Service" of undefined
  • Passport-init.js authentication is not working in nodejs
  • Branch.io Cordova API - init() fails on Android Device
  • appending HTML codes to an elements using js
  • Css animation to extend button text on hover
  • I want to restrict HTML input autocomplete to 3 letter minimum
  • tinyMCE editor not updating on the fly
  • typescript kendo-ui call method from string
  • HttpCompileException was unhandled by user code (when i am adding c# code in script)
  • Send form data as array of objects to controller in asp.net mvc
  • Javascript, multi file modules and Require.Js
  • how to render a complete new template and leave the application template in ember
  • Hover unrelated divs
  • Permute string until it matches some input?
  • How to fire JQuery change event when input value changed programmatically?
  • AJAX: POST data to a different url
  • checkbox column in jquery datatable
  • Document.evaluate for documents without namespaceURI crashes Microsoft Edge
  • Bootstrap Menu with "More" option Responsive issue
  • Standard inputs not being added to Angularjs Form $Scope
  • onclick and get in jquery
  • How to get the country code from Google Places API
  • Passing parameters url in Select2
  • Is there any frame work for Marklogic to write javascript code?
  • How to implement a boostrap template over bootstrap-sass gem
  • Why fixed div comes out when horizontally scrolled?
  • How to limit scope of a function in javascript/ jquery
  • Overlapping legend d3.js
  • Apply transition to div
  • Javascript game help - counting with dice
  • How to hide a div from angular controller
  • Can Promise load multi urls in order?
  • How enable Hover event for bootstrap menus?
  • D3 scale.invert() isn't returning date for x
  • Parsing object to array using underscore js or lodash
  • ES6 with React and Browserify
  • AngularJS Factory return type undefined
  • javascript follow html input fields
  • how to declare a double array in javascript for this particular UI
  • Jquery : find length of mixed list
  • Returning the array before the function is complete & Angular JS
  • Javascript set value of class & hidden input in nested divs
  • Register custom filters with vue.js
  • Add class to input parent
  • Angular controller not talking to view
  • Trouble creating "if then" Javascript statement to change background color of page
  • Socket.IO - Middleware and scoping: how should I access global IO object from external middleware?
  • add class when input is valid in angularjs
  • Positioning multiple div elements
  • Checkbox clicking works, deselecting checkbox not working Javascript
  • Adding ajax jQuery File upload
  • Creating a dynamic array within a dynamic array in JQuery
  • Multiple File Upload actions for a single File Input PHP/JS
  • Want to move an added element to a different part of the DOM
  • Why can't I get Angularjs to work?
  • Minimizing browser window in Firefox & Chrome adds white space to right of page
  • I need to insert the date and time of employee stamp in and out into mysql database
  • Making css auto slider
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co