简体   繁体   English

与两个API(nodejs,angular,express,mysql)的关系

[英]Relationship with two APIs (nodejs, angular, express, mysql)

I am a beginner in angular / node and I have a question that maybe can be simple (I believe) ... 我是angular / node的初学者,我有一个问题可能很简单(我相信)...

I was following the "Tour of Heroes" tutorial and started to wonder if it would be possible to have a type of "Contract" 我一直在关注“英雄之旅”教程,开始怀疑是否有可能使用“合同”类型

Contract would receive the data of the api Hero (id, name ...) and also data of another api "Company" (id, name, location ...) and then join everything in a called api "Contract". Contract将接收api Hero的数据(id,名称...)以及另一个api“ Company”的数据(id,名称,位置...),然后将所有内容都加入一个称为api的“ Contract”中。

Then in that case I could list "Contract ID" or "contract number", and see the information (which company, which hero, that kind of thing) 然后,在这种情况下,我可以列出“合同编号”或“合同编号”,然后查看信息(哪个公司,哪个英雄,那种东西)

Is it possible to do this with node, angular and mysql? 是否可以用node,angular和mysql做到这一点? if it is ... Could you show me some example or tutorial to follow? 如果是...您能告诉我一些例子或教程吗?

Thank you! 谢谢!

That is possible and easy, you just need a few things set up: 这是可能且容易的,您只需要进行一些设置即可:

  1. Your db needs a contract table (assuming you want a many to many relationship between heroes and companies) where you set foreign keys to the hero_id and the company_id . 您的数据库需要一个contract表(假设您要在英雄和公司之间建立多对多关系),您需要在其中设置hero_idcompany_id外键。
  2. You need a new endpoint in your server that receives the contractId as a parameter and then you can do a request to your mysql db that uses your contract table to join the company to the hero, ie 您需要在服务器中接收一个新的终结点,该终结点接收contractId作为参数,然后您可以向您的mysql数据库发送请求,该请求使用您的合同表将公司加入英雄联盟,即

    SELECT * FROM hero AS h JOIN contract AS cont ON h.id = cont.hero_id JOIN company AS comp ON comp.id = cont.company_id WHERE cont.id = ? ;

(replace the ? with the contract id you receive from the endpoint, ideally using prepared statements for mysql or a similar solution like the one mysqljs provides ) (将?替换为从端点收到的合同ID,最好使用针对mysql的准备好的语句或类似mysqljs提供的类似解决方案)

Angular will just need to call the new endpoint with a contractId parameter and you should receive the data if it exists. Angular只需要使用contractId参数调用新端点,并且您应该接收数据(如果存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM