所以基本上,我是 Javascript 的新手,只是想知道为什么我不能
- 从数据库中获取一些信息
- 将该信息与一些本地信息相结合,以制作一个大型 JSON 集
- 调用时将其发送到另一个函数
// This function fetches some data from a db and combines it with local data
async function axer (){
// Gets database info
const resp = await axios.get('http://localhost:3004/api/prod');
// Local modification to it
resp.data.spec = analyticsDashboardAppDB.widgets;
Returns the big final json file to the function below this
return await resp.data;
}
// This function sends it off when called by another function in a different js file
mock.onGet('/api/analytics-dashboard-app/widgets').reply( async config => {
await axer().then(async r => {return [200, await r]})
});
当我运行它时,我不断收到一条错误消息
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'config')
at handle_request.js:87:1
如果我从配置中删除异步和相关的等待,我会收到以下错误
Cannot read properties of undefined (reading 'then')
当我在发送之前打印 json 文件时,它会完全按照我的需要记录它,但是在我发送它之后,只有数据库信息存在,没有本地修改。不过,在我发送它之前和之后的长度是相同的,这让我相信一旦它被另一个函数调用,对 json 的本地修改就会出现未定义。
我很确定这与我对承诺的不完全理解有关,所以如果有人可以帮助我解决这个问题,我将不胜感激。