Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在一个函数中对数据库进行多个knex调用_Mysql_Node.js_Database_Api_Knex.js - Fatal编程技术网

Mysql 如何在一个函数中对数据库进行多个knex调用

Mysql 如何在一个函数中对数据库进行多个knex调用,mysql,node.js,database,api,knex.js,Mysql,Node.js,Database,Api,Knex.js,我有两个相互依赖的knex呼叫。第一个调用返回股票代码所在的行业,第二个调用返回表中数据库中具有相同行业的所有股票代码 我想在1函数中实现这一点,但我不知道在文档中搜索它时会调用什么。对于NodeJS/Knex来说还是很新的,所以任何建议都将不胜感激 我使用的数据库是MYSQL和NodeJS作为我的后端 这就是代码当前作为两个独立调用的外观: api.js router.get('/v1/symbols/:ticker/finn', async (req, res, nxt) => {

我有两个相互依赖的knex呼叫。第一个调用返回股票代码所在的行业,第二个调用返回表中数据库中具有相同行业的所有股票代码

我想在1函数中实现这一点,但我不知道在文档中搜索它时会调用什么。对于NodeJS/Knex来说还是很新的,所以任何建议都将不胜感激

我使用的数据库是MYSQL和NodeJS作为我的后端

这就是代码当前作为两个独立调用的外观:

api.js

router.get('/v1/symbols/:ticker/finn', async (req, res, nxt) => {
  try {
    const { ticker } = req.params;
    const finn = await industryApi.companyIndustry(ticker);

    res.json(finn[0].finnhubIndustry);
  } catch (err) {
    nxt(err);
  }
});

router.get('/v1/symbols/:finnhubIndustry/ind', async (req, res, nxt) => {
  try {
    const { finnhubIndustry } = req.params;
    const ind = await industryApi.IndustryList(finnhubIndustry);

    res.json(ind);
  } catch (err) {
    nxt(err);
  }
});
database.js

exports.companyIndustry = (ticker) => {
  return knex('company_data').where('ticker', ticker).select({
    finnhubIndustry: 'finnhubIndustry',
  });
};

exports.IndustryList = (industry) => {
  return knex('company_data').where('finnhubIndustry', industry).select({
    finnhubIndustry: 'finnhubIndustry',
    logo: 'logo',
    ticker: 'ticker',
    name: 'name',
  });
};
router.get返回如下内容:

http://localhost:3500/v1/symbols/AAPL/finn

"Technology"
http://localhost:3500/v1/symbols/Technology/ind

[{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/87cb30d8-80df-11ea-8951-00000000092a.png","ticker":"AAPL","name":"Apple Inc"},
{"finnhubIndustry":"Technology","logo":null,"ticker":"ALJJ","name":"ALJ Regional Holdings Inc"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/9564945e-80df-11ea-9144-00000000092a.png","ticker":"ALLT","name":"Allot Ltd"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/95ff91b4-80df-11ea-a942-00000000092a.png","ticker":"ALOT","name":"AstroNova Inc"},
{"finnhubIndustry":"Technology","logo":null,"ticker":"ALRM","name":"Alarm.com Holdings Inc"},
{"finnhubIndustry":"Technology","logo":null,"ticker":"ALTR","name":"Altair Engineering Inc"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/af289222-80da-11ea-a616-00000000092a.png","ticker":"ALYA.TO","name":"Alithya Group Inc"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/a393333e-80df-11ea-819f-00000000092a.png","ticker":"ASUR","name":"Asure Software Inc"},
{"finnhubIndustry":"Technology","logo":null,"ticker":"ATEN","name":"A10 Networks Inc"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/b9784a8c-80df-11ea-9db3-00000000092a.png","ticker":"BSQR","name":"Bsquare Corp"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/bbbad626-80d4-11ea-a7d3-00000000092a.png","ticker":"COUP","name":"Coupa Software Inc"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/ee4f7d8a-80cc-11ea-a29d-00000000092a.png","ticker":"CPAH","name":"CounterPath Corp"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/e248265c-80da-11ea-8f87-00000000092a.png","ticker":"DAVA","name":"Endava PLC"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/b9b56470-80eb-11ea-bde2-00000000092a.png","ticker":"DBD","name":"Diebold Nixdorf Inc"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/2739c27e-80db-11ea-8235-00000000092a.png","ticker":"DBX","name":"Dropbox Inc"},
{"finnhubIndustry":"Technology","logo":"https://static.finnhub.io/logo/c5890824-80e0-11ea-ad20-00000000092a.png","ticker":"DDD","name":"3D Systems Corp"}]