简体   繁体   English

需要将MongoDB与Heroku和Node,Express结合使用的帮助

[英]Need help using MongoDB with Heroku & Node, Express

My project goal is to make a checklist-like web app where the user can see what's in the database and also update what's inside it. 我的项目目标是制作一个类似清单的Web应用程序,用户可以在其中查看数据库中的内容并更新其中的内容。

Database is simple. 数据库很简单。 Just listing favorite things. 仅列出最喜欢的东西。 Eg {"favorite":"Pie"},{"favorite":"Kitty"} 例如{“ favorite”:“ Pie”},{“ favorite”:“ Kitty”}

So far I've figured out how to connect to the database(MongoHQ) and show a single element of data. 到目前为止,我已经弄清楚了如何连接到数据库(MongoHQ)并显示单个数据元素。 I've been trying for hours but I'm stumped on: 我已经尝试了几个小时,但被困了:

  1. Getting the html file to write whatever data is in the database. 获取html文件以写入数据库中的任何数据。
  2. Writing the Post form to write to the database. 编写Post表单以写入数据库。

Please share some insights! 请分享一些见解!

Here's my code. 这是我的代码。

.html .html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <div>

  <h1>Notepad</h1>
  <p>Favorite things: {{favorite}}</p>
<!--  {% for favorite in doc %}                  <-this doesn't work!
  <p>Favorite things: {{favorite}}</p>
  {% endfor %}
-->  
  </div>
</body>

</html>

web.js web.js

var express = require('express');
var path = require('path');
var fs = require('fs');
var http = require('http');
var mongo = require('mongodb');
var MongoClient = mongo.MongoClient;
var Server = require('mongodb').Server;
var mongoUri = "mongodb://id:pass@ds------.mongolab.com:-----/heroku_app------";
var cons = require('consolidate');
var app = express(express.logger());

app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('port', process.env.Port || 8080);
app.use(express.bodyParser());

var mydatabase = null;
var myCollection = null;

MongoClient.connect(mongoUri, function(err, db){
    if(!err){
        console.log("We are connected to mongo_lab!");
    }
    mydatabase = db;
    mydatabase.createCollection('test2', function(err, collection){});

    myCollection = mydatabase.collection('favorites');
    //myCollection.insert({'favorite':'Blueberries'}, {w:1}, function(err, result){});
});

var mongoclient_server = new MongoClient(new Server('localhost', 27017));

function errorHandler(err, req, res, next){
    console.error(err.message);
    console.error(err.stack);
    res.status(500);
    res.send("Fix the dam Error!");
}
app.use(errorHandler);

app.get('/notepad', function(req, res, next){
    MongoClient.connect(mongoUri, function(err, db){
        mydatabase.collection("favorites").find().toArray(function(err, doc){
        if(err) throw err;
        console.log(doc);
            res.render('notepad', doc[0]);
        });
    });
});

var htmlfile = "index.html";
app.get('/', function(request, response) {
var html = fs.readFileSync(htmlfile).toString();
response.send(html);
});

var port = process.env.PORT || 8080;
app.listen(port, function() {
  console.log("Listening on " + port);
});
//mongoclient_server.open(function(err, mongoclient_server){   <--if I use this, heroku crashes
//    if(err) throw err;
//    app.listen(port);
//});

You're passing data from doc[0] as locals to your notepad view, so basically notepad locals will be what is in doc[0] . 您要将数据从doc[0]作为本地传递到notepad视图,因此基本上, notepad本地将是doc[0]

In your call to res.render() , you should specify that your documents reside in the doc local, like this: 在对res.render()调用中,应指定您的文档位于doc本地,如下所示:

res.render('notepad', {
  doc: doc
});

This way your {% for ... %} will work. 这样,您的{% for ... %}就可以使用。

Docs for res.render res.render文件

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

相关问题 需要帮助在节点中使用 express 识别 memory 泄漏 - Need help identifying memory leak using express in node 将 Node、Express 和 MongoDB 服务器部署到 heroku - Deploy Node, Express, and MongoDB server to heroku Heroku Node.js(express.js)应用程序在本地运行,但在使用MongoDB时在heroku上失败 - Heroku Node.js (express.js) App Works Locally but failed on heroku when using MongoDB 需要帮助在Node js中编辑MongoDB文档 - Need help editing MongoDB document in Node js 使用 MongoDB 的 Express 应用程序无法在 Heroku 上运行 - Express app using MongoDB not working on Heroku 需要帮助来消化使用express-handlebars node.js返回的JSON blob - Need help digesting JSON blob returned using express-handlebars node.js 在 heroku 中部署 node.js / express / mongoDB ( with typescript ) - Deploy node.js / express / mongoDB ( with typescript ) in heroku Node.js&Express:需要基于POST值重定向到特定页面(使用MongoDB) - Node.js & Express: Need to redirect to specific pages based on POST values (using MongoDB) 无法使用Node&Express(Jade,MongoDB,Express,Node)进行POST - Cannot POST using node & Express (Jade, MongoDB, Express, Node) Node.js/Discord.js/Mongodb 需要帮助使用关键字提取信息 - Node.js/Discord.js/Mongodb Need help pulling information using keywords
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM