在Express get请求中调用函数时,无法从函数中获取返回值。这是代码:
从JS文件导出的函数。
const someFunction = () => {
return 'Hello';
}
Server.js
app.get('/callSomeFunction', (req, res) => {
swipe.someFunction((results) => {
console.log(results);
res.send(results);
})
})
前端React.js文件。
callSomeFunction = () => {
console.log('Calling someFunction.');
axios.get('/callSomeFunction')
.then((res) => {
console.log(res);
})
}
我正在使用axios来处理来自客户端的HTTP请求。这是一个虚拟的示例,但是在运行我的实际代码时,NodeJS处理该函数,只是不会将其发送回前端。
someFunction
刚返回一个字符串,那么你传递给函数someFunction
在Server.js
将不会被调用。
完成后,您可以调用该函数someFunction
:
const someFunction = (callback) => {
callback('Hello');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句