【问题标题】:Angular2 posting Data to nested db(mongodb)Angular2将数据发布到嵌套数据库(mongodb)
【发布时间】:2017-07-14 06:38:37
【问题描述】:
//user schema
{
  "firstname": "string",
  "lastname": "string",
  "username": "string",
  "password": "string",
  "email": "string",
  "emailVerified": true,
  "status": "string",
  "created": "2017-03-22T19:39:26.530Z",
  "lastUpdated": "2017-03-22T19:39:26.531Z",
  "id": "string",
  "sites":{
      "site": {
           "sitename":"string",
           "sitepass":"string"
      }
   }
}

这是我的 mongodb 架构。 第一个对这一切都是全新的,不要有太多的想法。

我想从 angular2 向这个架构(用户->站点->站点->站点名称)发出一个帖子请求,但是他们没有找到正确的方法来进入它。

我已经完成了一切设置,我可以向用户->名字 || 发出帖子请求用户->姓氏,但无法向用户->站点->站点->站点名发帖或获取请求!!!!!!

<input ng-model="??????" >

【问题讨论】:

    标签: node.js mongodb angular database


    【解决方案1】:

    你没有提供任何细节,所以我只能猜测你有什么

    假设您有一个用户服务并且正在使用 HTTP 模块与您的后端通信,到达您想要去的地方的最快方法是拥有一个与您的用户架构具有相同架构的对象。

    用户服务 class UserService { constructor (private http: Http) {} createUser(user) { return this.http.post('/users', user) .map(res => res.json()) } }

    然后在你的组件中你可以像使用你的 userService 一样

    this.user = { "firstname": "Jordan", "lastname": "Smith", ... "sites":{ "site": { "sitename":"string", "sitepass":"string" } } } this.UserService.createUser(this.user);

    你的[(ngModel)]="user"在这种情况下。

    再一次,这是我能用你提供的信息做的最好的事情。

    确实注意到您有“站点”(复数),但它不是数组。也许这是你的问题?

    【讨论】:

      【解决方案2】:

      你是完全正确的。我做了这样的事情。因为你的回复“网站”应该是一个数组。那么我的架构现在是

      {
        "firstname": "string",
        "lastname": "string",
        "username": "string",
        "password": "string",
        "email": "string",
        "emailVerified": true,
        "status": "string",
        "created": "2017-03-22T19:39:26.530Z",
        "lastUpdated": "2017-03-22T19:39:26.531Z",
        "id": "string",
        "sites":[
            "site": {
                 "sitename":"string",
                 "sitepass":"string"
            }
         ]
      }

      ,之后的问题是我将如何使用 (ngModel)]="" 将数据传递到“SITENAAME”以及如何在 html 标签中显示数据。

      【讨论】:

        猜你喜欢
        • 2018-11-27
        • 2018-01-07
        • 1970-01-01
        • 2022-01-04
        • 2017-05-23
        • 2016-01-25
        • 1970-01-01
        • 2021-01-13
        • 1970-01-01
        相关资源
        最近更新 更多