【问题标题】:push values to sails.js collection将值推送到sails.js 集合
【发布时间】:2023-03-20 17:31:02
【问题描述】:

我在sails.js 的一个小项目中工作,我有两个模型:Empresa 和 Noticia,Empresa 模型有一个 Noticia 的集合,而 Noticia 模型有一个 empresa 关系:

Empresa.js

module.exports = {

  attributes: {
    nombre: {
        type: 'string'
    },
    activa:{
      type: 'boolean',
      defaultsTo: false
    },
    usuarios:{
      collection: 'User',
      via: 'empresa'
    },
    noticias:{
      collection: 'Noticia',
      via: 'empresa'
    }
  }
};

Noticia.js

module.exports = {

  attributes: {
    titulo:{
        type: 'string',
        required: true
    },
    texto:{
        type: 'string',
        required: true
    },
    publicada:{
      type: 'boolean',
      defaultsTo: false
    },
    empresa:{
      model: 'Empresa',
      via: 'noticias'
    }
  }

在前端我有一个 Angular 应用程序,它会将 Noticias 一个一个添加到 noticias 集合中,我该怎么做?

EmpresaNoticias angular.js 控制器

.controller('EmpresaUsuarioController', function ($scope, $routeParams, $location, Noticia, Empresa) {

    $scope.addNoticia = function(){
        $scope.newNoticia.empresa = $routeParams.EmpresaId;
        Noticia.save($scope.newNoticia, function(noticia){
            Empresa.update({id: $routeParams.EmpresaId}, {noticias: noticia.id}, function(){
                $location.path('/noticia/'+ noticia.id);
            })
        }, function(error){
            console.log(error);
        });
    };

});

我知道在 javascript 中存在用于向数组添加值的函数 push,我想在sails.js 中做类似的事情,谢谢

【问题讨论】:

    标签: javascript angularjs node.js sails.js waterline


    【解决方案1】:

    嗯,这是一个愚蠢的答案,D:我只需要稍微更改 angular.js 控制器,如下所示:

    .controller('EmpresaUsuarioController', function ($scope, $routeParams, $location, Noticia, Empresa) {
    
        $scope.addNoticia = function(){
            $scope.newNoticia.empresa = $routeParams.EmpresaId;
            Noticia.save($scope.newNoticia, function(noticia){
               $location.path('/noticia/'+ noticia.id);
            }, function(error){
          });
        };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 2016-03-23
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多