用AngularJS的方式给MongoLab添加一个新的模型字段

Adding a new model field to MongoLab the AngularJS way

本文关键字:一个 模型 字段 方式 AngularJS MongoLab 添加      更新时间:2023-09-26

我有一个名为clientMongoDB文档(在MongoLab.com上),这将用于跟踪健身比赛。每周都会有一个"称重"和一些其他细节跟踪。

我所坚持的是如何添加子文档client文档来跟踪使用AngularJS的结果。

我觉得这可能是一些明显的和基本的,我错过了,但我还没有弄清楚如何追加一个新的字段到现有的记录,数组或非数组。

以下是我认为的相关细节,但如果合适,请随时询问更多细节。

完整代码粘贴到http://pastebin.com/4tyRiKus

示例客户端(我可以毫不费力地添加一个客户端)

{
    "_id": {
        "$oid": "50eee69fe4b0e4c1cf20d116"
    },
    "firstName": "Jon",
    "lastName": "Doe"
}

现在从一个不同的模板中,通过_id (/detail/:cliendId)的工作,但我希望添加一个数据点的权重。

<form ng-submit="addWeighIn()">
    <input type="date" ng-model="date" required />
    <input type="text" ng-model="weight" placeholder="Weight" smart-float required />
    <input type="submit" class="btn btn-primary" value="Add" />
</form>

点击这里的Add按钮继续阅读下一部分。

控制处理的函数(这在MongoLab模块中触发并调用update())

$scope.addWeighIn = function () {
    console.log("addWeighIn: " + $scope.date + " : " + $scope.weight);
    // the below line is obviously wrong, just out of ideas.
    $scope.client.weights = [ { date: $scope.date, weight: $scope.weight } ];
    $scope.client.update(function () {
    });
};

控制台显示addWeighIn行,网络数据显示它发送了一个请求,但它实际上没有在文档中包含权重,并且与上面相同。

从$scope.addWeighIn()调用蒙古资源并触发

Client.prototype.update = function (cb) {
    console.log("mongolab: update: " + cb);
    return Client.update({ id: this._id.$oid },
        angular.extend({}, this, { _id: undefined }), cb);
};

通过update()发送的数据(缺少新的权重数据)

{
    "_id": {
        "$oid": "50eee69fe4b0e4c1cf20d116"
    },
    "firstName": "Jon",
    "lastName": "Doe"
}

这就是我要找的,在实际的数据库中,但我需要添加权重,相信我不理解一些基本的东西。

{
    "_id": {
        "$oid": "50eee69fe4b0e4c1cf20d116"
    },
    "firstName": "Jon",
    "lastName": "Doe"
    "weights": [
        { "date": "1/3/2013",
          "weight": 155 }
        { "date": "1/10/2013",
          "weight": 150 }
    ]
}

我有几个问题要问。

  1. 我可以在HTML代码中声明性地做到这一点,例如将ng-model设置为client.weights.date?例如:<input type="date" ng-model="client.weights.date" required />(不工作,但我看到是否有类似的方式)
  2. 如果不是声明式的,那么通过AngularJS正确的方式是什么?
  3. 我应该使用什么样的术语来查找这些内容?这似乎是我的主要挫折,找不到例子。

我在http://docs.mongodb.org/manual/applications/update/找到了$push描述,可能是我需要使用的,但我不确定如何使用这个 AngularJS的方式

我最终发现问题出在MongoLab资源上。

我把我的悲哀实现与https://github.com/pkozlowski-opensource/angularjs-mongolab上找到的实现交换,它工作了。

由于我努力寻找一个例子,这里是如何添加子文档。

<form ng-submit="addWeighIn()">
    <table class="table table-striped">
        <thead>
        <tr>
            <th>Date</th>
            <th>Weight</th>
            <td></td>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="weight in client.weighIn | orderBy:'date'">
            <td>{{weight.date}}</td>
            <td>{{weight.weight}}</td>
            <td></td>
        </tr>
        </tbody>
        <tfoot>
        <tr>
            <td>
                <input type="text" ng-model="date" placeholder="Date of weigh-in" required />
            </td>
            <td class="input-append">
                <input type="text" ng-model="weight" placeholder="Weight" smart-float required />
                <span class="add-on">lbs</span>
            </td>
            <td><input type="submit" class="btn btn-primary" value="Add" /></td>
        </tr>
        </tfoot>
    </table>
</form>
脚本

$scope.addWeighIn = function () {
    console.log("addWeighIn: " + $scope.date + " : " + $scope.weight);
    $weighIn = { date: $scope.date, weight: $scope.weight };
    // push to the existing array if it exists
    if ($scope.client.weighIn)
        $scope.client.weighIn.push($weighIn);
    // else create the field if it doesn't exist
    else
        $scope.client.weighIn = [ $weighIn ];
    $scope.client.update(function () {
        // do whatever after the update
    });
};