2017-04-20 132 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