简体   繁体   English

Mongodb Atlas 集合中没有收到数据,即使与应用程序建立了连接

[英]No data received in Mongodb Atlas collection, even though connection is established with app

I am using node.js and heroku to deploy my app online, with mongodb atlas as my database, but for some reason, no data is being collected in the collections section of the database. I am using node.js and heroku to deploy my app online, with mongodb atlas as my database, but for some reason, no data is being collected in the collections section of the database. I have checked the logs, and there is no connection error mentioned.我检查了日志,没有提到连接错误。

As you can see, the logs shows "database opened".如您所见,日志显示“数据库已打开”。

Here is the code for my app.js file:这是我的 app.js 文件的代码:

// --- LOADING MODULES
var express = require('express'),
mongoose = require('mongoose'),
body_parser = require('body-parser');

// --- INSTANTIATE THE APP
var app = express();

// --- MONGOOSE SETUP
mongoose.connect(process.env.CONNECTION || 'mongodb://localhost/sopp-survey'); 
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function callback() {
    console.log('database opened');
});

var emptySchema = new mongoose.Schema({}, { strict: false });
var Entry = mongoose.model('Entry', emptySchema);


// --- STATIC MIDDLEWARE 
app.use('/jspsych-6.0.5', express.static(__dirname + "/jspsych-6.0.5"));
app.use('/css', express.static(__dirname + "/css"));
app.use('/img', express.static(__dirname + "/img"));

// --- BODY PARSING MIDDLEWARE
app.use(body_parser.json()); // to support JSON-encoded bodies

// --- VIEW LOCATION, SET UP SERVING STATIC HTML
app.set('views', __dirname + '/');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

// --- ROUTING
app.get('/', function(request, response) {
    response.render('index.html');
});

app.get('/experiment', function(request, response) {
    response.render('experiment_pilot.html');
});

app.post('/experiment-data', function(request, response){
    Entry.create({
        "data":request.body
    });    
    response.end();
})
// --- START THE SERVER 
var server = app.listen(process.env.PORT, function(){
    console.log("Listening on port %d", server.address().port);
});

I have specified the env variable CONNECTION using the command:我使用以下命令指定了环境变量 CONNECTION:

heroku config:set CONNECTION=mongodb+srv://<username>:<password>@CLUSTER_URL/MY_DB_NAME?retryWrites=true&w=majority

This is my package.json file:这是我的 package.json 文件:

{
  "name": "jspsych_survey",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "body-parser": "^1.19.0",
    "ejs": "^3.0.1",
    "express": "^4.17.1",
    "mongoose": "^5.9.6",
    "nodemon": "^2.0.2"
  },
  "engines": {
    "node": "12.x"
  }
}

On running the app locally, everything seems to work fine, even the data gets sent to the local database, which I access using the Mongo shell.在本地运行应用程序时,一切似乎都运行良好,甚至数据也被发送到本地数据库,我使用 Mongo shell 访问该数据库。

Is there something wrong with the code?代码有问题吗? Can anyone suggest what I can do to resolve this issue?谁能建议我可以做些什么来解决这个问题? Any help will be greatly appreciated.任何帮助将不胜感激。

Hi From the warning messeges in logs in seems that yo are using some deprecated method to connect your application.嗨,从登录中的警告消息看来,您正在使用一些不推荐使用的方法来连接您的应用程序。

So, use the new method to connect to your application.因此,使用新方法连接到您的应用程序。

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

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