Mean.js Stack-export.read,带有findOne和find函数,在Angularjs中调用

Mean.js Stack - export.read with findOne and find functions, call in Angularjs

本文关键字:函数 Angularjs 调用 find findOne Stack-export js read 带有 Mean      更新时间:2023-09-26

我是MEAN.js的新手,我对如何使用export.read和调用添加了find函数的findOne函数有点困惑。我的应用程序中有一个customers模块和一个customer-users模块。我有customers.server.controller、customerusers.server.coontroller和客户端控制器。客户就像一个组织。然后是属于客户的用户。我可以在customer-users模块中使用export.list单独显示客户用户列表。我可以在customers模块中显示客户列表,当然,当您选择客户时,我会看到客户视图。我想做的是,当您选择一个客户时,客户用户列表将显示在客户视图中客户名称的下方。我已经尝试编辑customer.server和customer.client控制器来完成这项工作,但我不确定正确的方法。

这是我的customers.server.controller.js:

'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors.server.controller'),
Customer = mongoose.model('Customer'),
passport = require('passport'),
User = mongoose.model('User'),
CustomerUser = mongoose.model('CustomerUser'),
_ = require('lodash');
/**
* Show the current Customer
*/
exports.read = function(req, res) {
    Customer.findOne({ _id: req.params.customerId }).exec(function(err, customer) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            CustomerUser.find({ customer: req.customer }).sort('-created').exec(function(err, customerUsers) {
                if (err) {
                    console.log('error');
                    return res.status(400).send({
                        message: errorHandler.getErrorMessage(err)
                    });
                } else {
                    var customerUsersList = customerUsers;
                    var selectedCustomer = customer;
                    var customerWithUsers = {
                        customer: selectedCustomer,
                        customerUsers: customerUsersList
                    };
                    console.log('1 : ' + customerUsersList);
                    console.log('2 : ' + selectedCustomer);
                    console.log('3 : ' + customerWithUsers);
                    res.jsonp(customerWithUsers);
                }
            });
        }
    });
};

我一直在尝试用所有的console.log调用调试所有的代码,一切似乎都为我提供了我想要的合适的数组。所以我觉得这应该在服务器端工作。

我的customers.client.controller.js(到目前为止;这是我对如何正确执行这一操作感到困惑的地方):

use strict';
// Customers controller
angular.module('customers').controller('CustomersController', ['$scope', '$stateParams', '$location', 'Authentication', 'Customers', 'AdminCustomerUsers', 'CustomerUsers',
function($scope, $stateParams, $location, Authentication, Customers, AdminCustomerUsers, CustomerUsers) {
    $scope.authentication = Authentication;
    $scope.customerId = $stateParams.customerId;
    $scope.findOne = function() {
        $scope.customer = Customers.get({
            customerId: $stateParams.customerId
        });
        $scope.customerUsers = Customers.query({
            customer: $scope.customer
        });
        console.log($scope.customer);
        console.log($scope.customer.customerUsers);
        console.log($scope.customerUsers);
    };

我尝试过很多不同的东西,所以这不是我所拥有的范围,但这是我为个人客户调用findOne的基础。

我的view-customers.client.view.html(当然必须根据工作解决方案进行修订):

 <section data-ng-controller="CustomersController" data-ng-init="findOne()">
    <div class="page-header">
        <h1 data-ng-bind="customer.name"></h1>
    </div>
    <div class="pull-right" data-ng-show="((authentication.user) && (authentication.user._id == customer.user._id))">
        <a class="btn btn-primary" href="/#!/customers/{{customer._id}}/edit">
            <i class="glyphicon glyphicon-edit"></i>
        </a>
        <a class="btn btn-primary" href="/#!/customer-users/{{customer._id}}/create">
            <i class="glyphicon glyphicon-user"></i>
            <i class="glyphicon glyphicon-plus"></i>
        </a>
        <!-- <a class="btn btn-default btn-danger" data-ng-click="remove();">
            <i class="glyphicon glyphicon-trash"></i>
        </a> -->
    </div>
    <small>
        <em class="text-muted">
            Added on
            <span data-ng-bind="customer.created | date:'mediumDate'"></span>
            by
            <span data-ng-bind="customer.user.displayName"></span>
        </em>
    </small>
    <br /><br /><br />
    <div class="list-group">
        <div data-ng-repeat="customerUser in customerUsers" class="list-group-item">
            <h4 class="list-group-item-heading">
                {{customerUser.user.displayName}}
                hello
                <small class="label label-default" ng-show="customerUser.admin">admin</small>
            </h4>
        </div>
    </div>
</section>

所以我的问题是如何在客户视图下获得客户用户列表?我做错了吗?有更好的方法吗?正如我所说,一切似乎都在服务器端工作,但我不能在客户端调用填充了正确对象的customerUsers数组。有没有一种方法可以在视图中与CustomersController一起使用CustomerUsersCoontroller,这样我就可以使用客户用户控制器来实现这一点?因为我也试过那样做,但失败得很惨。我可能只是错过了这里的逻辑。提前感谢您的帮助。

是否有从客户端调用服务器(node.js/customers.server.controller.js)的代码?

由于该调用将是异步的,因此Customers.get()和Customers.query()函数应该返回promise。Angular不会自动打开这些。

像这样的东西应该起作用:

Customers
    .get({
        customerId: $stateParams.customerId
    })
    .then(function(customer){
        $scope.customer = customer;
    });