Node.js 未处理的PromisejectionWarning:TypeError:res.send不是函数
我正在努力让这个aligoapi npm包工作。 当我发送请求时,它显示错误 这是我的密码:Node.js 未处理的PromisejectionWarning:TypeError:res.send不是函数,node.js,api,npm,promise,Node.js,Api,Npm,Promise,我正在努力让这个aligoapi npm包工作。 当我发送请求时,它显示错误 这是我的密码: const aligoapi = require('aligoapi'); var AuthData = { key: '<my_key>', user_id: '<my_id>', } var send = (req, res) => { console.log('check') aligoapi.send(req, AuthData) .th
const aligoapi = require('aligoapi');
var AuthData = {
key: '<my_key>',
user_id: '<my_id>',
}
var send = (req, res) => {
console.log('check')
aligoapi.send(req, AuthData)
.then((r) => {
console.log('check1')
res.send(r)
})
.catch((e) => {
console.log('check2')
res.send(e)
})
}
console.log('check3')
var number = {
body: {
sender: '01022558877',
receiver: '01081079508',
msg: 'test msg'
}
}
const result = send(number,'err')
console.log(result)
在这里,我注意到我的请求实际上没有被发送,因为它返回的是未定义的您定义了一个
send()
函数,该函数接受两个参数(req,res)
,但随后您像send(number,'err')
一样调用它。您传递的res
是一个字符串。您不能将其用作res.send()
,因为您传递的err
字符串没有.send()
方法。您必须将参数传递给函数,使其与函数的期望值以及这些参数的期望值相匹配。通常名为(req,res)
的参数是HTTP请求或快速路由处理程序的一部分,而不是您自己创建的。正是这些快速路由处理程序可以执行res.send()
。我如何澄清.send()
所需的参数?在普通Javascript中,无法强制输入参数。如果您想强制执行类型安全,可以使用TypeScript(编译为纯Javascript)。在纯Javascript中,您将使用参数命名和注释/文档来传达参数应该是什么。在某些情况下,您还可以在函数内部实现类型检查,以便在获取错误类型的参数时引发有意义的异常。
justin@Justinui-MacBookPro examples % node test.js
check3
check
undefined
check2
(node:94270) UnhandledPromiseRejectionWarning: TypeError: res.send is not a function
at /Users/justin/Downloads/node.js_exampl/examples/test.js:19:11
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:94270) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:94270) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
justin@Justinui-MacBookPro examples %