TypeError: data.filter is not a function
TypeError: data.filter is not a function
我正在尝试过滤 array
个 JSON
个对象,这些对象是我从 proxy
的 API
调用中获得的。我正在使用 Node.js
网络框架 Express
进行 API
调用。
API returns 如下:
{
data: [
{
type: "aaa",
name: "Cycle",
id: "c949up9c",
category: ["A","B"]
},
{
type: "bbb",
name: "mobile",
id: "c2rt4Jtu",
category: ["C","D"]
},
...
]
}
server.js
function sortDataByID(data) {
return data.filter(function(item) {
return item.id == 'c949up9c';
});
}
app.get('/products', (req, res) => {
const options = {
url: BASE_URL + '/products',
headers: {
'Authorization': 'hgjhgjh',
'Accept': 'application/json'
}
}
request.get(options).pipe(sortDataByID(res));
});
我不断收到以下错误消息。
TypeError: data.filter is not a function
这里明显的错误是什么?任何人?
我认为你的错误是认为 res
比你预期的 data
。
但是如果你往里面看 res
你应该会找到 data
.
所以你必须从 res
中获取 data
并使用它。
例如:
const data = res.data;
request.get(options).pipe(sortDataByID(data))
祝你有愉快的一天!
我个人从未见过函数的管道。我不认为这应该工作。无论如何:
您可以使用回调而不是管道。试试这个:
app.get('/products', (req, res) => {
const options = {
url: BASE_URL + '/products',
json: true, //little convenience flag to set the requisite JSON headers
headers: {
'Authorization': 'hgjhgjh',
'Accept': 'application/json'
}
}
request.get(options, sortDataByID);
});
function sortDataByID(err, response, data){ //the callback must take 3 parameters
if(err){
return res.json(err); //make sure there was no error
}
if(response.statusCode < 200 || response.statusCode > 299) { //Check for a non-error status code
return res.status(400).json(err)
}
let dataToReturn = data.data.filter(function(item) { //data.data because you need to access the data property on the response body.
return item.id == 'c949up9c';
}
res.json(dataToReturn);
}
我收到 TypeError:data.filter 在进行单元测试时不是一个函数。
我在结果中传递的是对象而不是数组。 gateIn$: of({}),而不是 gateIn$: of([]),
gateIn$.pipe(takeUntil(this.destroy$)).subscribe(bookings => (this.dataSource.data = bookings));
一旦你看到错误就很明显了,困难的是首先发现它。
我正在尝试过滤 array
个 JSON
个对象,这些对象是我从 proxy
的 API
调用中获得的。我正在使用 Node.js
网络框架 Express
进行 API
调用。
API returns 如下:
{
data: [
{
type: "aaa",
name: "Cycle",
id: "c949up9c",
category: ["A","B"]
},
{
type: "bbb",
name: "mobile",
id: "c2rt4Jtu",
category: ["C","D"]
},
...
]
}
server.js
function sortDataByID(data) {
return data.filter(function(item) {
return item.id == 'c949up9c';
});
}
app.get('/products', (req, res) => {
const options = {
url: BASE_URL + '/products',
headers: {
'Authorization': 'hgjhgjh',
'Accept': 'application/json'
}
}
request.get(options).pipe(sortDataByID(res));
});
我不断收到以下错误消息。
TypeError: data.filter is not a function
这里明显的错误是什么?任何人?
我认为你的错误是认为 res
比你预期的 data
。
但是如果你往里面看 res
你应该会找到 data
.
所以你必须从 res
中获取 data
并使用它。
例如:
const data = res.data;
request.get(options).pipe(sortDataByID(data))
祝你有愉快的一天!
我个人从未见过函数的管道。我不认为这应该工作。无论如何:
您可以使用回调而不是管道。试试这个:
app.get('/products', (req, res) => {
const options = {
url: BASE_URL + '/products',
json: true, //little convenience flag to set the requisite JSON headers
headers: {
'Authorization': 'hgjhgjh',
'Accept': 'application/json'
}
}
request.get(options, sortDataByID);
});
function sortDataByID(err, response, data){ //the callback must take 3 parameters
if(err){
return res.json(err); //make sure there was no error
}
if(response.statusCode < 200 || response.statusCode > 299) { //Check for a non-error status code
return res.status(400).json(err)
}
let dataToReturn = data.data.filter(function(item) { //data.data because you need to access the data property on the response body.
return item.id == 'c949up9c';
}
res.json(dataToReturn);
}
我收到 TypeError:data.filter 在进行单元测试时不是一个函数。
我在结果中传递的是对象而不是数组。 gateIn$: of({}),而不是 gateIn$: of([]),
gateIn$.pipe(takeUntil(this.destroy$)).subscribe(bookings => (this.dataSource.data = bookings));
一旦你看到错误就很明显了,困难的是首先发现它。