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

时间:2021-08-30 18:30:36

My relevant code is split into three files:

我的相关代码分为三个文件:

user.server.model.js:

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

mongoose.model('User', UserSchema);

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:

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. 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.

我需要做的是获取设备数组并将其以一种方式或其他方式传递到我的索引页面,然后我可以使用ejs处理它。由于我没有JavaScript经验,我没有找到明确的解决方案,所以我决定将数据传递给全局变量'devices',然后将其传递给索引页控制器。

Now the problem I am facing is that getDevices function does not change the global variable '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.

现在我遇到的问题是getDevices函数不会改变全局变量'devices'。我肯定知道的是,res.json(devices)在普通json中给出了设备数组的正确结果,并且全局变量'devices'的默认值(123)被传递给我的索引页控制器并呈现在页。

Any other suggestion how my need should be solved is greatly appreciated.

非常感谢任何其他建议如何解决我的需要。

Thank you very much.

非常感谢你。

1 个解决方案

#1


0  

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. In users.server.controller.js, do exports.devices = 123 and exports.devices = req.user.devices. Then, users.devices will work correctly in index.server.controller.js.

您需要导出设备,就像使用getDevices方法一样。在users.server.controller.js中,执行exports.devices = 123和exports.devices = req.user.devices。然后,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. 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.

编辑:但是从这样的方法返回一个值不是一个非常好/优雅的解决方案,似乎getDevices应该返回设备数组。如果您决定这样做的原因是因为Mongoose的findOne在回调中返回结果,请考虑在getDevices级别使用另一个回调或查看promises。

#1


0  

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. In users.server.controller.js, do exports.devices = 123 and exports.devices = req.user.devices. Then, users.devices will work correctly in index.server.controller.js.

您需要导出设备,就像使用getDevices方法一样。在users.server.controller.js中,执行exports.devices = 123和exports.devices = req.user.devices。然后,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. 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.

编辑:但是从这样的方法返回一个值不是一个非常好/优雅的解决方案,似乎getDevices应该返回设备数组。如果您决定这样做的原因是因为Mongoose的findOne在回调中返回结果,请考虑在getDevices级别使用另一个回调或查看promises。