问题是jsonFileGet函数返回“ undefined”。 我试图用一些假设来解决这个问题。
jsonFileGet()
返回了指针,因此当函数完成时,包含数据的内存将被删除但是我仍然无法解决问题。
1 const axios = require('axios');
2 const express = require('express');
3 const app = express()
4 const port = 8000;
5 const restapi = axios.create({
6 baseURL: "http://localhost:3001"
7 });
8
9 const jsonFileGet = async id => {
10
11 restapi.get(`/streams/${id}`)
12 .then(function(res){
13 const responedJson = res.data;
14 const passing = {...responedJson};
15 console.log(passing);
16 return passing;
17 }).catch( error => {console.log(`error! message:${error}`);});
18 }
19
20 app.get('/streams/:id',async (req, res) => {
21 const data = await jsonFileGet(req.params.id);
22 console.log(data);
23 res.send(data);
24
25 });
26
27 app.get('/streams',(req, res) => {
28 const jsonStream = jsonFileGet();
29 console.log(jsonStream);
30 res.send(jsonStream.data);
31 });
32
33 app.listen(port, ()=> console.log(`connecting:${port}`));