Stripe.create functions not in correct order - javascript

I want to integrate Stripe into my application. I have collected all necessary Data in the req.body.
Now an undefined error is being thrown after creating the customer Id and while trying to pass it to addCustomerToCard. After that, createToken is being successfully logged.
So two questions:
1.Why is the order of functions not being invoked as I would expect?
2.Why does the customer not get passed in addCustomerToCard?
router.post("/checkout", async function (req, res, next) {
if (!req.session.cart) {
return res.redirect("/shopping-cart");
}
let createCustomer = function () {
var param ={};
param.email = req.body.email;
param.name= req.body.name;
param.description ="";
return stripe.customers.create(param, function (err, customer) {
if (err) {
console.log("err:" + err);
}
if (customer) {
console.log("success: " + JSON.stringify(customer, null, 2));
} else {
console.log("something went wrong");
}
});
};
let createToken = function () {
let param ={};
param.card = {
number: req.body.card,
exp_month: req.body.exp_month,
exp_year: req.body.exp_year,
cvc: req.body.security
}
return stripe.tokens.create(param, function (err, token) {
if (err) {
console.log("err:" + err);
console.log(param);
}
if (token) {
console.log("success: " + JSON.stringify(token, null, 2));
console.log(req.body);
} else {
console.log("something went wrong");
}
});
};
let addCardToCustomer = function () {
console.log(createdCustomer);
return stripe.customers.createSource(customer.id, {source: token.id}, function (err, card) {
if (err) {
console.log("err:" + err);
console.log(param);
}
if (card) {
console.log("success: " + JSON.stringify(card, null, 2));
} else {
console.log("something went wrong");
}
});
};
let chargeCustomerThroughCustomerID = function () {
let param = {
amount: cart.totalPrice,
currency: 'eur',
description: 'First payment',
customer: customer.id
}
stripe.charges.create(param, function (err, charge) {
if (err) {
console.log("err: " + err);
}
if (charge) {
console.log("success: " + JSON.stringify(charge, null, 2));
} else {
console.log("Something wrong")
}
})
}
try {
const createdCustomer = await createCustomer(); // promise 1
const createdToken = await createToken();
const addedCardToCustomer = await addCardToCustomer(createdCustomer,createdToken ); //
// const chargeCustomerThroughCustomerID = await chargeCustomerThroughCustomerID(); // promise 4
res.send("success");
} catch (e) {
console.log(`error ${e}`)
};
});
//LOG OUTPUT
//success
//error ReferenceError: createdCustomer is not defined
//success

You should break this down into smaller pieces to verify the behaviour you're expecting, for starters. There's quite a lot going on here.
One problem is that you are trying to await functions which are not async -- you should have each function marked:
let createCustomer = async function () {...}
This is likely the reason the sequence is not what you expect.
You're hitting an error in addCardToCustomer because createCustomer is undefined in the scope of that function. You need to have a parameter:
let addCardToCustomer = async function (createdCustomer) { ... }

Related

PUT request crashes the server

I'm getting this error when trying to update an entry (in this case, checking a todo and setting it to true/false)
It seems like i'm getting the proper object (seen in the data console log) which makes me wonder why i'm getting [object Object].
Would appreciate any kind of help!
Here's the action :
export function onSaveTodo(todo) {
return async (dispatch) => {
try {
const savedTodo = await todosService.saveTodo(todo);
const action = {
type: 'UPDATE_TODO',
todo: savedTodo,
};
dispatch(action);
} catch (err) {
console.log('cant save todo');
}
};
}
the service :
const BASE_URL = 'todo';
async function saveTodo(todo) {
try {
if (todo._id) {
return httpService.put(BASE_URL, todo);
} else {
return httpService.post(BASE_URL, todo);
}
} catch (err) {
console.log(`could not save todo `, err);
throw err;
}
}
Http service :
export const httpService = {
get(endpoint, data) {
return ajax(endpoint, 'GET', data);
},
post(endpoint, data) {
return ajax(endpoint, 'POST', data);
},
put(endpoint, data) {
console.log('endpoint:', endpoint);
console.log('data:', data);
return ajax(endpoint, 'PUT', data);
},
delete(endpoint, data) {
return ajax(endpoint, 'DELETE', data);
},
};
The backend controller :
async function updateTodo(req, res) {
try {
const { todo } = req.body;
// console.log('todo:', todo);
const savedTodo = await todoService.update(todo);
res.json(savedTodo);
} catch (err) {
logger.error('Failed to update todo', err);
res.status(500).send({ err: 'Failed to update todo' });
}
}
and the backend service :
async function update(todo) {
try {
const newTodo = {
...todo,
_id: ObjectId(todo._id),
};
console.log('newTodo:', newTodo);
const collection = await dbService.getCollection('todo');
await collection.updateOne({ _id: newTodo._id }, { $set: newTodo });
return todo;
} catch (err) {
logger.error(`Can not update toy ${todo._id}`, err);
throw err;
}
}

UNHANDLEDREJECTION Callback was already called in aysnc parallel

I am using async parallel to get data from db in parallel.
When every task is returning the data I am storing it in local object.
From index.js I am calling cacheService.js .
In cacheService.js I am loading data from mysql database and mongo database in to cache object.
Whenever I am doing npm run local run. I am getting following error.
UNHANDLEDREJECTION Error: Callback was already called.
This error is coming from loadMongoData method of cacheService.js.
I have followed other answers on stackoverflow like adding else part
Here is code for cacheService.js.
'use strict';
var cache = [];
class cacheService {
async init() {
await this.loadMongoData();
}
loadMongoData(env, callback1) {
const _this = this;
console.log('Inside loadMongoData')
async.parallel(
{
task1: function (callback) {
CriteriaDef.find({})
.lean()
.exec(function (err, criteriaDefs) {
if (err) {
console.log('Inside err 1')
logger.error('Error fetching CriteriaDef: ', util.inspect(err));
callback(err, null);
} else if (criteriaDefs) {
console.log('Inside criteriaDefs')
if (criteriaDefs.length && criteriaDefs.length > 0) {
console.log('Inside criteriaDefs 1')
global.CRITERIA_DEFS = criteriaDefs;
cache['criteria_defs'] = criteriaDefs;
}
callback(null, null);
}
});
},
task2: function (callback) {
groupDef
.find({})
.lean()
.exec(function (err, groupDefs) {
if (err) {
console.log('Inside err2')
logger.error('Error fetching groupDefs: ', util.inspect(err));
callback(null, err);
} else if (groupDefs) {
console.log('Inside ')
global.groupDefsWithRoles = groupDefs;
let _groupDefs = [];
_.each(groupDefs, function (groupDef) {
var data = {
value: groupDef._id,
label: `${groupDef._id}: ${groupDef.description}`
};
_groupDefs.push(data);
});
global.groupDefs = _groupDefs;
cache['groupDefs'] = _groupDefs;
logger.info('Loaded groupDefs: ', global.groupDefs.length);
callback(null, null);
}
});
},
task3: function (callback) {
jiraProjects.find({$or: [{archived: {$ne: true}}, {archived: {$exists: false}}]}).exec(function (err, jiraProjects) {
if (err) {
console.log('Inside error 3')
logger.error('Error fetching jiraProjects: ', err);
callback(null, err);
} else if (jiraProjects) {
console.log('Inside jira project')
let _jiraProjects = [];
_.each(jiraProjects, function (jiraProject) {
var data = {
value: jiraProject.key,
label: jiraProject.key,
issueType: jiraProject.issueType ? jiraProject.issueType : 'Bug'
};
_jiraProjects.push(data);
});
global.jiraProjectsList = _jiraProjects;
cache['jiraProjects'] = _jiraProjects;
logger.info('Loaded jira projects: ', global.jiraProjectsList.length);
callback(null, null);
}
});
},
task4: function (callback) {
console.log('Inside task4')
callback(null, null);
},
task5: function (callback) {
inputElements
.find({})
.lean()
.exec(function (err, inputElements) {
if (err) {
console.log('Inside error5')
logger.error('Error fetching inputElements: ', util.inspect(err));
callback(null, err);
} else if (inputElements) {
console.log('Inside inputelements')
global.INPUT_ELEMENTS_DEF = inputElements;
cache['INPUT_ELEMENTS_DEF'] = inputElements;
callback(null, null);
}
});
},
task6: function (callback) {
console.log('Inside task6')
sisp.loadProducts('', callback);
}
},
function (err, results) {
if (err) {
console.log('Inside final error')
logger.error("Something went wrong can't start the app: ", util.inspect(err));
callback1(null, err);
} else {
console.log('Inside final else')
logger.info('loaded all globals properly :)');
callback1(null, null);
}
}
)
}
}
export default new cacheService();
I think your problem cuz you use callback inside of promise.
Please change your code like this:
class cacheService {
async init() {
await new Promise((resolve, reject) => {
this.loadMongoData(env, (ignoreArg, error) => {
if (error) {
return reject(error);
}
resolve();
});
});
}
...
}
Tip: in the each task add statement else for call callback, because maybe your statement not handled and your code can't execute prefect
task1: function (callback) {
CriteriaDef.find({})
.lean()
.exec(function (err, criteriaDefs) {
if (err) {
console.log('Inside err 1')
logger.error('Error fetching CriteriaDef: ', util.inspect(err));
callback(err, null);
} else if (criteriaDefs) {
console.log('Inside criteriaDefs')
if (criteriaDefs.length && criteriaDefs.length > 0) {
console.log('Inside criteriaDefs 1')
global.CRITERIA_DEFS = criteriaDefs;
cache['criteria_defs'] = criteriaDefs;
}
callback(null, null);
} else {
callback(null, null);
}
});
},

Promise returns undefined json in Express post request

I have a promise within a selectRecipientData function that returns some user data from an api.
export async function selectRecipientData({ email }) {
engage.selectRecipientData({
listId: listId,
email: email,
returnContactLists: false,
}, function(err, result) {
if(err) {
console.log(err);
} else {
let recipient = JSON.stringify(result);
// this logs successfully
console.log('Recipient details: ' + recipient );
return recipient;
}
});
}
When I call this function within a post request. The data is logged within the promise but is undefined when returned as per below:
server.post('/api/v1/public/selectrecipientdata', async (req, res) => {
formData = req.body;
let { email } = formData;
if (!email) {
res.json({ error: 'Email is required' });
return;
}
try {
let recipientData = await selectRecipientData({ email });
// why is this undefined?
console.log('This is Undefined: '+ JSON.stringify(recipientData) );
res.json({recipientData});
} catch (err) {
res.json({ error: err.message || err.toString() });
}
});
Anyone tell me why? Thanks
You've written selectRecipientData as a callback style function, but you're calling it as an async/await style. If engage.selectRecipientData returns a promise, you could do something like:
export async function selectRecipientData({email}) {
const result=await engage.selectRecipientData({
listId: listId,
email: email,
returnContactLists: false,
});
const recipient=JSON.stringify(result);
console.log('Recipient details: ' + recipient );
return recipient;
}
Otherwise, to convert it to a promise you could do something like:
export function selectRecipientData({email}) {
return new Promise((resolve,reject)=>{
engage.selectRecipientData({
listId: listId,
email: email,
returnContactLists: false,
}, function(err, result) {
if (err) {
reject(err);
}
else {
let recipient = JSON.stringify(result);
console.log('Recipient details: ' + recipient);
resolve(recipient);
}
});
});
}

NodeJS Async Database fetch server freezing

I have an application running on NodeJS(express + mongoose + jade).
I have a post-route /search (all routes are in a separate module) which should handle fetching data from mongo database and inserting it into jade template(in this case just printing th console):
router.post('/search', function (req,res) {
var componentsArray = null;
function getArray(){
console.log('Initializing...');
componentsArray = dataExchanger.search(req.body.select, req.body.selectType, req.body.searchField);
}
getArray(function () {
console.log('Documents returned.');
console.log('Printing array...');
console.log('Array: ' + componentsArray);
console.log('Array type: ' + typeof (componentsArray));
console.log('Rendering page...');
res.render('search_results');
});
});
Searching and fetching function implemented in a different module dataExchanger:
exports.search = function(select, type, data) {
console.log('Fetching documents...');
componentsModel.find({name: data}, function (err, docs) {
if(!err) {
console.log('Returning documents...');
return docs;
} else {
console.log('Can\'t return documents!');
throw err;
}
});
};
The problem is that when I am using a callback function for getArray(), the server just freezes at the moment of returning docs and stops responding.
What am I doing wrong?
Try to use async/await
router.post('/search', async (req,res) => {
let componentsArray;
try {
componentsArray = await dataExchanger.search(req.body.select, req.body.selectType, req.body.searchField);
} catch(e){
//If error in request and no data.
console.error('Error', e.message);
return res.render('error_message');
}
console.log('Documents returned.');
console.log('Printing array...');
console.log('Array: ' + componentsArray);
console.log('Array type: ' + typeof (componentsArray));
console.log('Rendering page...');
res.render('search_results');
});
And here is your dataExchanger
exports.search = function(select, type, data) {
console.log('Fetching documents...');
return new Promise((resolve, reject) => {
componentsModel.find({name: data}, function (err, docs) {
if(err) return reject(err);
resolve(docs);
});
})
};
Further reading: promises, async/await
router.post('/search', function (req,res) {
var componentsArray = null;
function getArray(cb){
console.log('Initializing...');
componentsArray = dataExchanger.search(req.body.select, req.body.selectType, req.body.searchField);
//Execute the callback
cb();
}
getArray(function () {
console.log('Documents returned.');
console.log('Printing array...');
console.log('Array: ' + componentsArray);
console.log('Array type: ' + typeof (componentsArray));
console.log('Rendering page...');
res.render('search_results');
});
});
Looks like your search method is async as well, so you will need to pass the callback down to that to get the desired result.

Don't work function with promise

I try this code:
function addNewCars(req, res) {
let CarsList = req.body;
carListParsing(carList)
.then(function () {
console.log('OK');
res.status(200).send('OK');
}).catch(function (err) {
res.status(200).send(err);
});
}
function carListParsing (data) {
return new Promise(function (resolve, reject) {
let newCar = {};
newCar.name = data.car_name;
validateCar(newCar).then(function (data) {
console.log('validate result1: ', data); //this line doesn't show
//if I get validation result, I can use next function createCat()
resolve(data);
}).catch(function (err) {
reject(err);
});
});
}
function validateCar(data) {
db.cars.findAll({where: {name: data.name}}).then(function (org) {
if (org.length < 1) {
console.log('validate1: OK'); //work
return data;
} else {
console.log('validate2: already exist'); //work
return new Error('The ' + data.name + ' car is already exist.');
}
}).catch(function (err) {
return err;
});
}
I neet to validate data => Car name, if car not exist then create new car and perform next logic, example park car. If car found, then perform function park car.
Avoid the Promise constructor antipattern in carListParsing, and return your result promise from validateCar!
function addNewCars(req, res) {
// CarsList is a typo
carListParsing(req.body)
.then(function () {
console.log('OK');
res.status(200).send('OK');
}, function (err) { // more appropriate than catch
res.status(200).send(err);
});
}
function carListParsing (data) {
let newCar = {
name: data.car_name
};
// don't call `new Promise`
return validateCar(newCar).then(function (data) {
console.log('validate result1: ', data); //this line doesn't show
// if I get validation result, I can use next function createCat()
return data; // don't resolve
});
}
function validateCar(data) {
return db.cars.findAll({where: {name: data.name}}).then(function (org) {
// ^^^^^^
if (org.length < 1) {
console.log('validate1: OK'); //work
return data;
} else {
console.log('validate2: already exist'); //work
throw new Error('The ' + data.name + ' car is already exist.');
// ^^^^^ probably what you actually wanted
}
}) // don't ignore errors
}

Categories

Resources