Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检索和使用MongoDB数组_Javascript_Node.js_Ajax_Mongodb - Fatal编程技术网

Javascript 检索和使用MongoDB数组

Javascript 检索和使用MongoDB数组,javascript,node.js,ajax,mongodb,Javascript,Node.js,Ajax,Mongodb,我正在使用连接到MongoDB数据库的Node.js服务器构建一个网站 对于其中一个页面,我打算从Mongo数据库获取一个数组,并使用它在网页上动态生成一个显示 我已经在我的网站的另一部分实现了使用数组构建动态显示,但这是使用我在JavaScript文件中生成的数组 我正在努力弄清楚的是如何从Mongo数据库检索数组,然后在JavaScript文件中使用它 .ejs页 <div class="col-sm-12 text-center cocktail-text"> <h2&g

我正在使用连接到MongoDB数据库的Node.js服务器构建一个网站

对于其中一个页面,我打算从Mongo数据库获取一个数组,并使用它在网页上动态生成一个显示

我已经在我的网站的另一部分实现了使用数组构建动态显示,但这是使用我在JavaScript文件中生成的数组

我正在努力弄清楚的是如何从Mongo数据库检索数组,然后在JavaScript文件中使用它

.ejs页

<div class="col-sm-12 text-center cocktail-text">
<h2>My Cocktails: <%= user.login.username %></h2>
</div>

<div class="myCocktails">
<div>
<div class="row text-center">
  <!-- Dynamically generated elements will go here -->

  <!-- Example of element once generated:
  <div class="col-sm myCocktailBackground">
    <img src="img/Mojito.jpg" alt="..." class="img-thumbnail">
    <h3>Mojito</h3>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  </div> -->

</div>
</div>
</div>

我的鸡尾酒:
我已经成功地从Mongo数据库中提取数据并将其显示在.ejs页面上,我只是不知道如何在JavaScript文件中访问它。如果这是最好的方法,我也一直在使用AJAX。

我的解决方案:

我最终使用ajax
GET
访问我的节点服务器,在我的服务器上搜索mongo数据库以查找正确的用户,并在数据库中返回与该用户名相关的数据。然后,我解析javascript文件中的数据以获得我想要的数组

JavaScript文件:

$.ajax({

 type: 'GET',
 url: '/saveddrinks',
 dataType: "json",
 cache: false,
 contentType: "application/json",
 success: function(data) {
   if (data) {
     //Printing data to console
     console.log(data);
     //Printing array to console;
     console.log(data.drinks);
     var drinksarray = data.drinks;
     //***WORK WITH ARRAY HERE***
    }
   else {
     console.log("no data");
    }
  }
});
Server.js

app.get('/saveddrinks', function(req, res) {
 //If user is not currently logged in, redirect them to login page.
 if(!req.session.loggedin){res.redirect('/login');return;}
 //get the requested user based on their username?
 //set username of logged in user
 var uname = sess.username;
 //this query finds the first document in the array with that username.
 //Because the username value sits in the login section of the user data we 
 //use login.username
 db.collection('users').findOne({
   "login.username": uname
   }, function(err, result) {
   if (err) {res.send(err)}
   console.log(uname + ":" + result);
   //finally we just send the result to the user page as "user"
   res.send(result);
 });
});

如果使用Express,只需在呈现页面时发送数组:
res.render('your_page',{data:your_array})
之后,您可以在EJS视图中的名称
data