2017-04-20 157 views
0

我试图打电话给通过的Restlet API保存的搜索和我有我的Restlet部署,但试图运行它的时候,我发现了以下错误:NetSuite公司的Restlet错误代码:INVALID_RETURN_DATA_FORMAT

error code: INVALID_RETURN_DATA_FORMAT

error message:Invalid data format. You should return TEXT.

通过我的脚本查看,我无法确定错误的位置。以下是我正在尝试运行的脚本

function GetSearchResult(){ 
    //array container for search results 
    var output = new Array(); 

    //get search results 
    var results = nlapiSearchRecord('transaction','customsearchid',null,null); 
    var columns = results[0].getAllColumns(); 

    //loop through the search results 
    for(var i in results){ 
     //create placeholder object place holder 
     var obj = new searchRow(
      //set the values of the object with the values of the appropriate columns 
      results[i].getValue(columns[0]), 
      results[i].getValue(columns[1]), 
      results[i].getValue(columns[2]), 
      results[i].getValue(columns[3]), 
      results[i].getValue(columns[4]), 
      results[i].getValue(columns[5]), 
      results[i].getValue(columns[6]), 
      results[i].getValue(columns[7]), 
      results[i].getValue(columns[8]), 
      results[i].getValue(columns[9]), 
      results[i].getValue(columns[10]), 
      results[i].getValue(columns[11]), 
      results[i].getValue(columns[12]) 
     ); 

     //add the object to the array of results 
     output.push(obj); 
    } 

    //return the array of search objects 
    return output; 
} 

//Object to serve a place holder for each search row 
function searchRow(internalid,lineid,subsidiaryid,locationid,departmentid,accountid,date,name,memo,amount,uniqueid,product,period){ 
    this.internalid = internalid; 
    this.lineid = lineid; 
    this.subsidiaryid = subsidiaryid; 
    this.locationid = locationid; 
    this.departmentid = departmentid; 
    this.accountid = accountid; 
    this.date = date; 
    this.name = name; 
    this.memo = memo; 
    this.amount = amount; 
    this.uniqueid = uniqueid; 
    this.product = product; 
    this.period = period; 

} 

谢谢!

回答

2

请确保您发送了Content-Type标题application/json与您的请求。我相信你还需要return JSON.stringify(output);而不仅仅是return output;

你也不想在数组中使用JavaScript的for..in;它是only meant for Object iteration。相反,您应该使用标准for循环。

+1

添加内容类型标头完美地工作,谢谢! – Ryan

3

这个问题只是你需要请求中的内容类型的application/json。

for..in可以正常工作。 NetSuite的搜索结果并不总是表现为真正的数组,所以我会经常做

var x = (nlapiSearchRecord(...) || []); 

x.forEach(function(r){ 

}); 

至于JSON去我刚刚返回的对象。我从来没有遇到错误 - 如果我没有送一个Content-Type头的GET请求我得到一个错误 - 它可能是您的客户端程序的设定内容键入text/plain的,这就是为什么NS预计文本输出。

只需稍后续处理: 如果您发送头文件,即使在GET请求中,Netsuite也会将您的查询字符串(如果有的话)解释为键值对的对象,并将其传递给你的函数AND会将你返回的对象作为JSON传递,所以不需要自己调用JSON.stringify。

如果不设置头,那么你就必须调用JSON.parse在返回的数据数据和JSON.stringify传递的,如果你希望你的客户端程序来接收JSON。这有点奇怪,因为标准方式是让Netsuite查看Accept:头并适当地表达数据。 Netsuite确实在他们的帮助中拼出了这个(排序):https://system.na1.netsuite.com/app/help/helpcenter.nl?fid=section_4619215993.html

+0

添加内容类型标头完美运行,谢谢! – Ryan

+0

嗨瑞安。很高兴工作。请将答案标记为已接受。我已经添加了一些后续 – bknights