AnguarJS和MongDB(CRUD)保存/更新 - 不工作

时间:2017-11-23 04:19:08

标签: javascript angularjs mongodb

我有一个从mongoDB重复数据的表单:

  • 每个数据连续返回 - firstname,lastname和 移动
  • 用户有3种选择 - 编辑,保存和删除

编辑和删除都通过指向client._id来工作。它能够获取特定的行,但是" save"不管用。我认为有一个基本的概念,即ng-repeat和ng-click,我没有正确理解

查看

@api.multi
    def onchange_segus1(self,segus1):
    values          = {}
        segus_obj    = self.env['segus.list']
    if segus1:
        data = segus_ob.browse(segus1).sDescr
        values  = {
            'decrTecn' : data       
        }
    return {'value': values} 

控制器

class Cheese(Model.models):
    type = models.CharField(max_length='50', unique=True)

class Sauce(Models.models):
    type = models.CharField(max_length='50', unique=True)

class Pizza(Models.models):
    cheese = models.ForeignKey(Cheese)
    sauce = models.ForeignKey(Sauce)

    class Meta:
         unique_togather=(("cheese", "sauce"),)

服务

<form name="updateClientForm" ng-repeat="(clientIndex, client) in ctrl.clients" novalidate>

            <div class="row" style="margin-top: 10px">

                <div class="col-md-3 col-md-offset-1">
                    <div class="form-group">
                        <input name="firstname" class="form-control" type="text" ng-model="client.firstname" required>
                    </div>
                </div>

                <div class="col-md-3">
                    <div class="form-group">
                        <input name="lastname" class="form-control" type="text" ng-model="client.lastname" required>
                    </div>
                </div>

                <div class="col-md-3">
                    <div class="form-group">
                        <input name="mobile" class="form-control" minlength="8" maxlength="8" type="text" ng-model="client.mobile" required>
                    </div>
                </div>

                <div class="col-md-3">
                        <button type="button" ng-click="ctrl.editClient(client._id)" class="btn-sm btn-warning">
                                <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                            </button>
                        <button name="button" ng-click="ctrl.saveClient(client)" class="btn-sm btn-success">
                                <i class="fa fa-check-square" aria-hidden="true"></i>
                            </button>
                            <button type="button" ng-click="ctrl.deleteClient(client._id); ctrl.removeRow(clientIndex);" class="btn-sm btn-danger">
                                <i class="fa fa-trash" aria-hidden="true"></i>
                            </button>
                </div>

            </div>

        </form>

路线

//SAVE
self.saveClient = function (client) {

  console.log("Saving client... ");
  console.log('client - before calling paAppAPI: ', client); //Returns array with client data

  paAppAPI.updateClient(client)
  .then(function (result) {
    console.log(result);

    self.message = result.data;
    self.showSuccessMessage = true;

  }).catch(function (err) {
    console.log(err);
    self.message = err.data;
    self.showFailureMessage = true;
  });

}

任何建议都会有所帮助! :)

1 个答案:

答案 0 :(得分:0)

在您的表单中,进行以下更改 - 将client传递给ctrl.saveClient <button name="button" ng-click="ctrl.saveClient(client)" class="btn-sm btn-success"> <i class="fa fa-check-square" aria-hidden="true"></i> </button>

在Controller中,在self.saveClient中进行以下更改

//SAVE
self.saveClient = function (client) {

  console.log("Saving client... ");
  console.log('client - before calling paAppAPI: ', client);
  paAppAPI.updateClient(client)
  .then(function (result) {
    console.log(result);

    self.message = result.data;
    self.showSuccessMessage = true;

  }).catch(function (err) {
    console.log(err);
    self.message = err.data;
    self.showFailureMessage = true;
  });

}

在服务中,在self.updateClient

中进行以下更改
//SAVE
    self.updateClient = function (client) {
        var defer = $q.defer();
        const id = client._id;
        console.log('client - before calling http: ', client);  
        console.log('id- before calling http: ', id);
        $http.put("/api/client/" + id, {client: client })
            .then(function (result) {
                console.log(result);
                defer.resolve(result);

            }).catch(function (err) {
                defer.reject(err);
            });

        return defer.promise;

    }         

在服务器上,更改api以进行更新,如下所示

//SAVE
app.put("/api/client/:id", function (req, res) {

console.log(req.params.id); //returns undefined
console.log(req.body.client); //returns {}

    Client.updateOne({ "_id" : req.params.id }, { $set: req.body.client }, (err, result) => {
        if (err) {
            console.log(err);
            handleError(err, res);
            return;
        }

        res.status(200).json({msg: 'Successfully saved'});
    });
});