Reputation: 3466
I am migrating my App from MFP V 7.1 to MFP V 8.0. I have following scenario in which I am constructing a data object and sending the request to the MFP adapter from my client using below code:
function SearchData(username,name,city){
this.username = username;
this.name = name;
this.city = city;
}
searchData(){
var searchData = new SearchData(getUserId(),name,city);
var dataRequest = new WLResourceRequest('/adapters/MyAdapterName/searchEmployer', WLResourceRequest.GET);
alert('there');
dataRequest.send(searchData).then(
function(response){
console.log('response --> ' + response);
},
function(){
console.log('error response --> ' );
}
);
}
Below is the code written in my java script adapter:
function searchData(searchData){
try{
WL.Logger.info("Inside searchData() method.");
var input = {
method : 'post',
returnedContentType : 'json',
path : 'rest/search',
body : {
contentType : 'application/json;charset=utf-8',
content : JSON.stringify(searchData)
}
};
var response = MFP.Server.invokeHttp(input);
return response;
}catch(exception){
WL.Logger.error("Inside searchData() method :: " + exception.message);
throw exception;
}
}
Every time when I am calling this method, the failure function is getting called. I also tried to send the request using sendFormParameters method but it is returning printing following error:
worklight.js:9342 Uncaught Error: Invalid invocation of method WLResourceRequest.sendFormParameters; Form value must be a simple type.
logAndThrowError @ worklight.js:9342
encodeFormParameters @ worklight.js:9727
WLResourceRequest.sendFormParameters @
worklight.js:9685searchEmployer @ VM79:47
onclick @ index.html:1
Upvotes: 0
Views: 941
Reputation: 2118
That issue occurs when the object you are passing contains nested objects or is of type function.
In your case you have multiple variables/functions with the same name searchData
, SearchData
. I'll recommend you change the names to be more descriptive.
I've tried the following and it runs successfully
function SearchData(username,name,city){
this.username = username;
this.name = name;
this.city = city;
}
function search(){
var data = new SearchData(getUserId(), name, city);
var request = new WLResourceRequest('/adapters/MyAdapterName/searchEmployer', WLResourceRequest.GET);
request.send(searchData).then(function(response){
console.log('response --> ', response);
}, function(error){
console.log('error response --> ', error);
});
}
If you are still having problems after running the code above, share a link with your project or add more code snippets.
UDPATE:
For Javascript adapters receive their parameters via a GET variable params
, params
is an array of parameters.
So you'll need to update your code in the client as follow:
function search(){
var data = new SearchData(getUserId(), name, city);
var request = new WLResourceRequest('/adapters/MyAdapterName/searchEmployer', WLResourceRequest.GET);
request.setQueryParameter("params", [JSON.stringify(data)]);
request.send().then(function(response){
console.log('response --> ', response);
}, function(error){
console.log('error response --> ', error);
});
}
Since the data passed to the adapter is a sting the you need to update your adapter procedure to reflect that i.e., remove JSON.stringify
function searchData(payload){
try{
WL.Logger.info("Inside searchData() method.");
var input = {
method : 'post',
returnedContentType : 'json',
path : 'rest/search',
body : {
contentType : 'application/json;charset=utf-8',
content : payload
}
};
var response = MFP.Server.invokeHttp(input);
return response;
}catch(exception){
WL.Logger.error("Inside searchData() method :: " + exception.message);
throw exception;
}
}
Upvotes: 4