简体   繁体   English

从前端到后端发送关系数据更新

[英]Sending relational data updates from front-end to back-end

I have a table which holds different persons. 我有一张桌子,可以容纳不同的人。 Now the user can select a set of persons (say Tom Dick & Harry) and group them into a new "team". 现在,用户可以选择一组人员(例如Tom Dick和Harry)并将他们分组为新的“团队”。 In the backend i would need to update Tom, Dick & Harry and add a team-id to their team_field. 在后端,我需要更新Tom,Dick和Harry并在他们的team_field中添加一个团队ID。

eg: 例如:

{
name: "Tom",
age: 12,
team_id: id_for_team_1
}

But since team_1 did not exist before the user selected Tom,Dick & Harry and created the team, I do not yet have an id for team1. 但是由于在用户选择Tom,Dick&Harry并创建团队之前team_1不存在,所以我还没有team1的ID。

I need this first it seems: 我首先需要这个:

{
_id: id_for_team_1
team_name: "Team Cool"
team_color: "blue"
}

How would I solve this in the front-end? 我将如何在前端解决此问题?

My idea: Make a request to the backend to create the team on the server, get the team_id back from the backend, and then add the id to to Tom, Dick & Harry and then send their updates profiles to the backend? 我的想法是:向后端请求在服务器上创建团队,从后端获取team_id,然后将ID添加到Tom,Dick和Harry,然后将其更新配置文件发送到后端?

But: is this the simplest approach I could take, or are there better ways to solve this? 但是:这是我可以采用的最简单的方法,还是有更好的方法来解决这个问题? It seems complicated. 看起来很复杂。 Ideally , I would like to just update everything in the front-end, that is, create a team object + update profiles, and then send it all to the server in one go. 理想情况下 ,我只想更新前端的所有内容,即创建一个团队对象+更新配置文件,然后一次性将所有内容发送到服务器。 But I don't know how I could do this, since I seem to be dependent on mongodbs ids and cannot just make up my own in the frontend. 但是我不知道该怎么做,因为我似乎依赖于mongodbs id,不能仅仅在前端组成自己的ID。

In case you just want to update the teams they belong you can just in one call sending a list of userIds, and the backend can then create a new team and also update the team_id. 如果您只想更新他们所属的团队,则可以在一个呼叫中发送一个用户ID列表,然后后端可以创建一个新团队并更新team_id。 Pseudocode: 伪代码:

1) POST /api/team
2) let userIds = req.body.userIds
3) newTeamId = Create a new team in Mongo and get its id
4) for(uid in userIds){ 
    user = Mongo.find(id : uid);
    user.teamId = newTeamId 
    Mongo.update(user)
 }

If you also want to be able to update users information, I would create 2 API methods, one for updating the user profile and another to manage the teams. 如果您还希望能够更新用户信息,我将创建2种API方法,一种用于更新用户个人资料,另一种用于管理团队。

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

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