简体   繁体   English

如何使用Mongoose将数据传递给全局变量?

[英]How to pass data with Mongoose to a global variable?

My relevant code is split into three files: 我的相关代码分为三个文件:

user.server.model.js: user.server.model.js:

    var UserSchema = new Schema({
        ...            
        devices: []
    });

mongoose.model('User', UserSchema);

users.server.controller.js: users.server.controller.js:

var User = require('mongoose').model('User') 

var devices = 123;

exports.getDevices = function(req, res, next) { 
    User.findOne({
            _id: req.user.id
        },
        function(err, user) {
            if (err) {
                return next(err);
            }
            else {
                devices = req.user.devices;
                res.json(devices)                       
            }
        }
    )
}

exports.devices = devices;

index.server.controller.js: index.server.controller.js:

var users = require('../../app/controllers/users.server.controller')

exports.render = function(req, res) {   
    res.render('index', {
        title: 'MEAN MVC',
        user: req.user ? req.user.name: '',
        device: users.devices
    });
};

I am using node.js, mongoose, mongodb, express, embeddedjs. 我正在使用node.js,mongoose,mongodb,express,embeddedjs。

What I need to do is to get the devices array and pass it down one way or other to my index page where can I then handle it there with ejs. 我需要做的是获取devices数组并将其以一种或另一种方式传递到我的索引页,然后在其中使用ejs处理它。 As I am not experienced in JavaScript, I did not find a clear solution, so I decided to pass the data to a global variable 'devices' , then pass it to index page controller. 因为我没有JavaScript的经验,所以没有找到明确的解决方案,因此我决定将数据传递给全局变量“ devices” ,然后将其传递给索引页控制器。

Now the problem I am facing is that getDevices function does not change the global variable 'devices' . 现在我面临的问题是getDevices函数不会更改全局变量'devices' What I know for sure, is that res.json(devices) gives a correct result of devices array in plain json and that the default value (123) of the global variable 'devices' is passed down to my index page controller and rendered in page. 我确定知道的是, res.json(devices)在普通json中给出了devices数组的正确结果,并且全局变量'devices'的默认值(123)向下传递到我的索引页控制器并在页。

Any other suggestion how my need should be solved is greatly appreciated. 任何其他建议应如何解决我的需求,我们深表感谢。

Thank you very much. 非常感谢你。

devices isn't a global variable: it's only accessible from within the file that contains it, in your case users.server.controller.js . devices不是全局变量:只能从包含它的文件中访问(在您的情况下为users.server.controller.js

You need to export devices , just like you do with the getDevices method. 您需要导出devices ,就像使用getDevices方法一样。 In users.server.controller.js , do exports.devices = 123 and exports.devices = req.user.devices . users.server.controller.js ,执行users.server.controller.js exports.devices = 123exports.devices = req.user.devices Then, users.devices will work correctly in index.server.controller.js . 然后, users.devices将在正常工作index.server.controller.js

EDIT: However returning a value from a method like that is not a very good/elegant solution, it seems like getDevices should be returning the devices array. 编辑:但是从这样的方法返回一个值不是一个很好的/优雅的解决方案,似乎getDevices应该返回devices数组。 If the reason you decided to do it like this is because Mongoose's findOne returns the result in a callback, consider using another callback at the getDevices level or look into promises. 如果您决定这样做的原因是因为Mongoose的findOne在回调中返回了结果,请考虑在getDevices级别使用另一个回调或研究promises。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM