簡體   English   中英

Meanjs內置的應用程序,如何從文章模塊更新USER數組

[英]Meanjs built in app, how to update USER array from articles module

我正在將Articles and Users實現用於Meanjs內置應用程序。

帶有每個項目按鈕的文章列表(視圖):

<a data-ng-controller="MyArticlesController" data-ng-repeat="article in articles" class="list-group-item">
        <button data-ng-click="addArt2me()" class="glyphicon glyphicon-plus addArt2me" ></button>
        <h4 class="list-group-item-heading" data-ng-bind="article.title"></h4>
        <p> {{article.content | limitTo:140}} </p>
    </a>

這是帶有觸發功能$scope.addArt2me()的控制器:

'use strict';
angular.module('users').controller('MyArticlesController', ['$scope', 'Articles', 'Users',    '$location',
function($scope, Articles, Users, $location) {

    var myUser = new Users($scope.user);

    $scope.addArt2me = function() {

        var myArticle = new Articles($scope.article);

        myUser.userArticles.push(myArticle._id);

        myUser.$update(function(response) {
            console.log("Actualize!! con : " + user.userArticles.length + "__" + response);
        }, function(errorResponse) {
            console.log("updatError: " + errorResponse);
            $scope.error = errorResponse;
        });

    }
}
]);

在用戶模型中,我有一個articles._id userArticles數組。

視圖通過按鈕觸發addArt2me()的函數addArt2me()來呈現文章列表,該按鈕會在“ addArt2me()中推送並更新myArticle._id。

它可以成功工作,並將元素保存到DB中:)

console: Actualize!! con : 60__[object Object] 

...但是只有第一次,下次會觸發錯誤:(

PUT http://localhost:3000/users 400 (Bad Request)
updatError: [object Object]´

我需要處理某種服務來更新ARTICLES模塊中的USERS模塊嗎? 我不能只用Mongoose更新用戶的模型嗎? 為什么對於第一個保存的文章效果很好?

任何指南都非常感謝。 謝謝!

經過2天的尋找,我才成立。

需要在子控制器外部聲明用戶模型,因為它會針對每篇文章進行迭代。

因此,將“ $ scope.myUser = new Users($ scope.user);”移至“ ArticlesController”中!

謝謝你的時間!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM