Closed vijay-nts closed 6 years ago
Hi there, The problem is that your injection function requires asynchronicity, which means you need to supply a callback parameter to it instead of returning out of it. See http://www.mbtest.org/docs/api/injection for an example (search for "asynchronous" on the page to find the example).
The injection function's signature should be:
function GetTemplateResponse (request, state, logger, callback)
And then when you get your results from the CSV parsing, pass them to callback rather than returning.
Expected behaviour
Should return status code, headers and body : response ...
Actual behaviour
Keeps on Loading and not returning the values ...
Steps to reproduce
"responses": [ { "inject": "<%-stringify(filename, './GetProductInfo.js') %>" }
"predicates": [ { "matches": { "method" : "POST", "path" : "/details/search" } } ]
GetProductInfo.js function GetTemplateResponse (request, state, logger)
{ var productId; var response = JSON.parse("<%- stringify(filename, './es_productinfo.json') %>"); getUpdatedCSVData(function(data) { console.log('callback data >>>>>>>>: ', data);
// Process data and update response and return return {
statusCode : 200, headers: { 'Content-Type': 'application/json; charset=utf-8' }, body: response };
}
If return is given outside getUpdatedCSVData(function(data) function as below it is working but not able update the response from CSV data and it is giving default value. Also if we try to update outside getUpdatedCSVData function it is not happening as this statement is processed before getting data from csv as it is running asynchronously.
{ var productId; var data; var sleep = require('sleep'); var response = JSON.parse("<%- stringify(filename, './es_productinfo.json') %>");
return {
}
........
Software versions used
Log contents in mb.log when running mb --loglevel debug