我有一個函式,我正在使用axios將資料發布到nodeJS rest api。我遇到的問題是,axios添加了一個帶有物件的串列,而不是只有陣列物件。請幫助我解決這個問題。
該函式從 "documents "中接收follow。{
_id。'6149290b197615d32c515dab',
instantMessage: false,
isComplete: true,
},
{
_id: '614a249636503d7aa9fb138d',
instantMessage: false,
isComplete: true,
},
{
_id: '614a2bf5560184026def253a',
date: '2021-09-21',
title: 'not getting erro',
},
{
_id: '614a2c6a560184026def253d',
date: '2021-09-21',
title: 'Every thing working',
}
我的函式如下:
async function SaveAsTemplate(documents){
const result = await axios
.post('http:localhost/templates'/span>, {
檔案。
})
.catch(function (error) {
//處理錯誤。
console.warn(error.message)。
});
return result;
}
在我的nodeJS專案中,在收到查詢的地方,我在console.log資料,我得到了以下的結果:
documents: [
{
_id: '6149290b197615d32c515dab',
instantMessage: false,
isComplete: true,
},
{
_id: '614a249636503d7aa9fb138d',
instantMessage: false,
isComplete: true,
},
{
_id: '614a2bf5560184026def253a',
date: '2021-09-21',
title: 'not getting erro',
},
{
_id: '614a2c6a560184026def253d',
date: '2021-09-21',
title: 'Every thing working',
}
我怎樣才能使用axios,它只給我物件而不給前面的檔案。 當我使用Postman和其他工具發送查詢到post時,我沒有這個問題,一切都很正常。只有在使用axios時才有問題
uj5u.com熱心網友回復:async function SaveAsTemplate(documents){
const result = await axios
.post('http:localhost/templates'/span>, {
headers: {
'Content-Type': 'application/json',
},
body: 檔案。
})
.catch(function (error) {
//處理錯誤。
console.warn(error.message)。
});
return result;
}
或者你可以嘗試先將陣列改為物件,然后在正文中進行賦值
uj5u.com熱心網友回復:你正在做的是
const result = await axios
.post('http:localhost/templates'/span>, {
檔案。
})
這就等于:
const result = await axios
.post('http:localhost/templates'/span>, {
documents: 檔案。
})
嘗試:
const result = await axios
.post('http:localhost/templates'/span>, documents)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331517.html
標籤:
