2016-01-21 53 views
1

我正在傳遞2個JSON數組。但只有第一個JSON的數據插入Big Query中。任何人都可以請建議我哪裏錯了?我們可以在Big Query中使用post api請求插入多行嗎?

var req = { 
    method: 'POST', 
    url: 'https://www.googleapis.com/bigquery/v2/projects/pid/datasets/dataid/tables/tabid/insertAll', 
    headers: { 
     'Authorization': token1, 
     'Content-Type': 'application/json', 
     'scope': 'https://www.googleapis.com/auth/bigquery' 
    }, 
    json: { 
     "rows": [{ 
     "json": [{ 
      'code': 'X-new', 
      "remark": '', 
      'resulting_status': 'Cancelled' 
     }, { 
      'code': 'X-jdkdjk', 
      "remark": '', 
      'resulting_status': 'Required' 
     }] 
     }] 
    } 
    }; 

    console.log(JSON.stringify(req.json.rows)); 
    request(req, function(error, response, body) { 
    if (error) 
     debug("Error occurred from client's server" + error); 
    else 
     console.log("Response......" + JSON.stringify(response.body)); 
    }); 
+0

你忘了一個獨特的insertId。 – Pentium10

回答

2

我認爲你的請求正文是錯誤的。 「json」數組字段中有多個元素(實際上應該只是一個對象)。您應該在「行」字段中確實有多個元素。這是我覺得你的要求應該是這樣的:

json: { 
     "rows": [{ 
     // optional insert id here. 
     "json": { 
      'code': 'X-new', 
      "remark": '', 
      'resulting_status': 'Cancelled' 
     } 
     }, { 
     // optional insert id here. 
     "json": { 
      'code': 'X-jdkdjk', 
      "remark": '', 
      'resulting_status': 'Required' 
     } 
     }] 
    } 
相關問題