[英]Fail to retrieve data from MongoDb on Node.Js API
I have a MongoDb server hosted on Azure. 我在Azure上托管了MongoDb服务器。 I'm now building a Node.js API meant to retrieve data from a table on one of the databases (ie table: Word; database: MyDatabase). 我现在正在构建一个Node.js API,用于从其中一个数据库的表(即表:Word;数据库:MyDatabase)中检索数据。 I've built the API following this tutorial , but I'm unable to successfully retrieve any data from it... 我已按照本教程构建了API,但无法从中成功检索任何数据...
I know the server is up and running and also reachable since I can tcp-connect to it through: 我知道服务器已启动并正在运行,并且也可以访问,因为我可以通过以下方式进行tcp连接:
psping [Azure's Public IP]:27017
Now, I have an node.js api with the following code: 1) app/server.js 现在,我有一个带有以下代码的node.js api:1)app / server.js
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://[Azure's public IP]:27017/MyDatabase');
var Word = require('./models/word');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR API
var router = express.Router(); // get an instance of the express Router
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next();
});
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
router.route('/words')
.get(function(req, res) {
Word.find(function(err, words) {
if (err)
res.send(err);
res.json(words);
});
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);
I've also written a model for my only table within the database, which has 3 columns: the auto-generated ObjectId, Spanish, French (meant to have words in both languages to make it work as a translator). 我还为数据库中的唯一表编写了一个模型,该模型具有3列:自动生成的ObjectId,西班牙语,法语(意在同时使用两种语言的单词,使其能够用作翻译器)。 The models looks like this: 2) app/models/word.js 这些模型如下所示:2)app / models / word.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var WordSchema = new Schema({
spanish: String,
french: String
})
var Word = mongoose.model('Word',WordSchema);
module.exports = Word;
Now, I go to postman and GET on the following: http://localhost:8080/api/words ; 现在,我去邮递员和GET在以下: http:// localhost:8080 / api / words ; which returns []. 返回[]。
On MongoDb logs I see the following: 在MongoDb日志上,我看到以下内容:
2016-08-05T03:16:26.520+0000 I NETWORK [conn60] end connection [Some IP]:[Some port] (1 connections now open)
2016-08-05T03:31:11.878+0000 I NETWORK [initandlisten] connection accepted from [Some IP]:[Some port] #61 (1 connection now open)
I think you are missing {}
when doing find. 我认为您在进行查找时缺少{}
。
router.route('/words')
.get(function(req, res) {
Word.find({}, //Added here.
function(err, words) {
if (err)
res.send(err);
console.log(words)
res.json(words);
});
});
Hope this will help. 希望这会有所帮助。
EDIT:- 编辑:-
According the document of doc , the find function accept the first parameter as an object and treat it as conditions, but not a callback function. 根据doc的文档 ,find函数接受第一个参数作为对象并将其视为条件,但不接受回调函数。
As you mentioned in your comment that the documents were retrieved from db.word.find()
I think I found the problem. 正如您在评论中提到的,这些文档是从db.word.find()
检索的,我想我发现了问题。 You need to put documents into collection named words
, instead of word
. 您需要将文档放入名为words
而不是word
集合中。
Mongoose will use the plural version of your model name. 猫鼬将使用模型名称的复数形式 。 See http://mongoosejs.com/docs/models.html for more information. 有关更多信息,请参见http://mongoosejs.com/docs/models.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.