简体   繁体   English

如何在节点模块和另一个JavaScript文件中声明数组(Mongodb)

[英]How to Declare an array in a node module and another JavaScript file (Mongodb)

I am very new to node.js and programming in general. 我对node.js和一般编程非常陌生。 I am trying to learn to retrieve a variable's value from Mongodb. 我正在尝试学习从Mongodb检索变量的值。 I have the 'data' variable in my app.js 我的app.js中有'data'变量

var data = require ("./public/assets/js/data.js");
app.get('/test', function(req, res) {
    res.locals.place = data;
    console.log(data);

    res.render('ViewMode');
});

my data.js file looks like this: 我的data.js文件如下所示:

var mongoose = require ('mongoose');
var data = new Array();
mongoose.model('stories').find({},function(err, companies) {
    for (var i = 0; i < companies.length; i++) {
        data[i] = JSON.stringify(companies[i].place);
    }
});

module.exports = data;

and I want to use this in a JavaScript file I have for showing a map. 我想在显示地图的JavaScript文件中使用它。

var places = []
var places =  locals.place;

for (var i = 0; i < places.length; i++) {
    var mylocation = places[i];
    var lat = mylocation.replace(/^\"\(([0-9-.]*),.*/g, "$1");
    var lng = mylocation.replace(/.*,\s*([0-9-.]*)\)\"$/g, "$1");
    var latLng = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({
        position: latLng,
        map: map,
    });    
}  

I tried to use re.locals.variable but am not sure whether that's a right approach or not or do I need to have an ajax? 我尝试使用re.locals.variable,但不确定这是否正确,还是需要使用Ajax?

Thanks 谢谢

Wrap the Mongo stuff in a function in your data.js module 将Mongo内容包装在data.js模块的函数中

var mongoose = require ('mongoose');

function getPlaces(callback){
  var data = new Array();
  // this function here is async, so use a callback to "return" the result
  mongoose.model('stories').find({},function(err, companies) {
    if(err){
      return callback(err, data);
    }
    for (var i = 0; i < companies.length; i++) {
      data[i] = JSON.stringify(companies[i].place);
    }
    return callback(null, data);
 });
}

// then export the function
module.exports = getPlaces;

Then require the module in express and pass a function to it 然后要求表达模块并将功能传递给它

var placeFinder = require ("./public/assets/js/data.js");
app.get('/test', function(req, res){

  placeFinder(function(err, data){
    if(err){
      // Internal error!
      return res.send(500);
    }
    // your crazy code here to manipulate the data here

    res.locals.place = data;
    console.log(data);
    res.render('ViewMode');

  });
});

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

相关问题 如何使用Java监视节点中的另一个模块 - How to spy on another module in node with Javascript 如何在另一个文件中声明的JavaScript代码中声明变量? - How to declare variable in javascript code that is declared in another file? 如何在Javascript中声明一个byte数组 - How to declare an array of byte in Javascript 如何在 Node.js 中声明一个 static 数组? - How to declare a static array in Node.js? javascript中的节点js / MongoDB副本集数组 - Node js / MongoDB replica set array in javascript 数组递增Javascript / node js / mongodb - Array incrementing Javascript / node js / mongodb 如何在一个是模块的 Javascript 文件和另一个不是模块的 Javascript 文件之间共享函数和变量 - How to share functions and variables between one Javascript file that IS a module, and another Javascript file that is NOT a module 如何在JavaScript中将数组声明为class的数据成员 - How to declare array as a data member of a class in JavaScript 在node.js中,模块中的一个文件如何查看模块中另一个文件中的功能? - in node.js, how does one file in a module see functions in another file in the module? 如何在 javascript 中为这个特定的 UI 声明一个双数组 - how to declare a double array in javascript for this particular UI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM