Res.send 不是函数
Res.send not a function
我有一个端点(使用 express),需要我先进行一些提取。一旦解析响应并使用 res.send 我得到一个错误 res.send is not a function
.
我尝试搜索此错误,但所有搜索都显示用户 res,req
的顺序有误。在这种情况下,我的似乎是正确的。
为什么将我的回复转换为 JSON 后 res 不在范围内?
router.post("/customerID", async (req, res) => {
return fetch({endPoint}, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Flowspace-Auth": {myToken},
},
body: JSON.stringify({
query: `query {
user {
name
organizationId
}
}`,
}),
})
.then((res) => {
res.json().then((data) => {
console.log(data) // This works
res.send({ data: data }); // res.send is not a function... why, is it not scoped correctly?
});
})
.catch((err) => console.log("unable to fetch:", err));
});
您的外部 response
变量被内部 result
变量覆盖。 JS 从最内层到最外层寻找变量。由于 res
已在 then
子句中定义,因此使用 res
。
将其更改为 resp
应该可以。
router.post("/customerID", async (req, resp) => {
return fetch({endPoint}, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Flowspace-Auth": {myToken},
},
body: JSON.stringify({
query: `query {
user {
name
organizationId
}
}`,
}),
})
.then((res) => {
res.json().then((data) => {
console.log(data) // This works
resp.send({ data: data }); // resp will belong to outer response
});
})
.catch((err) => console.log("unable to fetch:", err));
});
您可能也想在 catch
部分发送一些内容。
您正在对发送方法不可用的获取 api 调用的响应调用发送方法。在下面找到正确的代码。
router.post("/customerID", async (req, res) => {
return fetch(
{ endPoint },
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Flowspace-Auth": { myToken },
},
body: JSON.stringify({
query: `query {
user {
name
organizationId
}
}`,
}),
}
)
.then((response) => {
response.json().then((data) => {
console.log(data); // This works
res.send({ data: data });
});
})
.catch((err) => console.log("unable to fetch:", err));
});
我有一个端点(使用 express),需要我先进行一些提取。一旦解析响应并使用 res.send 我得到一个错误 res.send is not a function
.
我尝试搜索此错误,但所有搜索都显示用户 res,req
的顺序有误。在这种情况下,我的似乎是正确的。
为什么将我的回复转换为 JSON 后 res 不在范围内?
router.post("/customerID", async (req, res) => {
return fetch({endPoint}, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Flowspace-Auth": {myToken},
},
body: JSON.stringify({
query: `query {
user {
name
organizationId
}
}`,
}),
})
.then((res) => {
res.json().then((data) => {
console.log(data) // This works
res.send({ data: data }); // res.send is not a function... why, is it not scoped correctly?
});
})
.catch((err) => console.log("unable to fetch:", err));
});
您的外部 response
变量被内部 result
变量覆盖。 JS 从最内层到最外层寻找变量。由于 res
已在 then
子句中定义,因此使用 res
。
将其更改为 resp
应该可以。
router.post("/customerID", async (req, resp) => {
return fetch({endPoint}, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Flowspace-Auth": {myToken},
},
body: JSON.stringify({
query: `query {
user {
name
organizationId
}
}`,
}),
})
.then((res) => {
res.json().then((data) => {
console.log(data) // This works
resp.send({ data: data }); // resp will belong to outer response
});
})
.catch((err) => console.log("unable to fetch:", err));
});
您可能也想在 catch
部分发送一些内容。
您正在对发送方法不可用的获取 api 调用的响应调用发送方法。在下面找到正确的代码。
router.post("/customerID", async (req, res) => {
return fetch(
{ endPoint },
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Flowspace-Auth": { myToken },
},
body: JSON.stringify({
query: `query {
user {
name
organizationId
}
}`,
}),
}
)
.then((response) => {
response.json().then((data) => {
console.log(data); // This works
res.send({ data: data });
});
})
.catch((err) => console.log("unable to fetch:", err));
});