2017-02-28 67 views
1

我是使用MongoDB的新手。目前,我正在制作一个需要一些数据存储的Web应用程序。我在运行localhost:3000的Node.js上建立了一个HTTP服务器。我还使用Vagrant和VirtualBox构建了一个虚拟开发环境。我正在从PuTTy访问Mongo shell(如果这相关的话)。在合并MongoDB之前,它正常工作,因为我将数据存储在Node.js程序内存中。无法从MongoDB数据库获取网络应用程序的数据

这个网络应用程序是一个在线的待办事项列表。我得到的第一个错误是得到的路线。我已经插入到Mongo数据库中的“待办事项”列表不会出现在本地主机的网站上。它似乎没有从数据库中获取数据。我得到的第二个错误是路线。当我通过本地主机上的用户界面插入“待办事项”并刷新页面时,localhost上的待办事项列表将更新为特定待办事项(但不包括我插入数据库中的待办事项)。但是,这似乎并没有将它添加到数据库中,我仍然得到这个错误在控制台上:

[email protected]:~/app$ node server.js 
{ description: 'descTest3', tags: [ 'tagTest3' ] } 
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html 

我不知道为什么我得到的错误,因为我似乎没有使用任何承诺。

server.js

var express = require("express"), http = require("http"), app = express(), mongoose = require("mongoose"); 

app.use(express.static(__dirname + "/client")); 

app.use(express.urlencoded()); 

mongoose.connect('mongodb://localhost/WatNext');//connect to the WatNext data store in mongoDB 

var ToDoSchema = mongoose.Schema({ 
    "description": String, 
    "tags": [ String ] //array of Strings 
}); 

var ToDo = mongoose.model("ToDo", ToDoSchema) 

http.createServer(app).listen(3000); 
//get and post routes: 

app.get("/toDos.json", function (req, res) { 
    ToDo.find({}, function (err, toDos) { 
     if (err != null){ 
      console.log(err); 
     } 
     res.json(toDos); 
    }); 
}); 

app.post("/todos", function (req, res) { 
    console.log(req.body); 
    var addedToDo = new ToDo({"description" : req.body.description, "tags" : req.body.tags}); 
    //save function saves object into the database 
    addedToDo.save(function (err, result) { 
     if (err != null){//if there is an error 
      console.log(err); 
      res.send("ERROR SAVING"); 
     } 
     else { 
      ToDo.find({}, function (err, result) { 
       if (err != null){//if there is an error in finding 
        res.send("ERROR FINDING"); 
       } 
       res.json(result); 
      }); 
     } 
    }); 
}); 

app.js

var main = function (toDoObjects) { 
//do stuff with ToDoObjects including outputting the list of ToDos 
}; 
$(document).ready(function() { 
    $.getJSON("toDos.json", function(toDoObjects) { 
     main(toDoObjects); 
    }) 
}); 

蒙戈壳

> show dbs 
WatNext 0.0625GB 
local 0.03125GB 
> use WatNext 
switched to db WatNext 
> show collections; 
system.indexes 
toDoCollection 
todos 
> db.toDoCollection.find(); 
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf1"), "description" : "Get groceries and eat afterwards", "tags" : [ "shopping", "chores" ] } 
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf2"), "description" : "Make up some new To-Dos", "tags" : [ "writing", "work" ] } 
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf3"), "description" : "Prep for Monday's class", "tags" : [ "work", "teaching" ] } 
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf4"), "description" : "Answer emails", "tags" : [ "work" ] } 
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf5"), "description" : "Take April to the park", "tags" : [ "chores", "pets" ] } 
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf6"), "description" : "Finish writing this book", "tags" : [ "writing", "work" ] } 

编辑: 我发现这只是一个错误的命名。

我还发现,

mongoose.Promise = require("bluebird"); 

解决了这个问题的承诺错误。请记住先安装模块:

npm install --save bluebird 
+0

当你从UI尝试/ POST方法,你可以通过命令行使用db.toDoCollection.find()看到结果吗? – Matt

+0

当我通过UI执行发布方法时,toDoCollection不会更新。我输入db.toDoCollection.find();但列出的唯一数据是我通过shell自己插入到数据库中的数据。但是,当我刷新时,UI会因新数据而更新,但出于某种原因。 –

+0

[(node:3341)DeprecationWarning:Mongoose:mpromise](http:// stackoverflow。com/questions/38138445/node3341-deprecationwarning-mongoose -mpromise) – Matt

回答

1

我发现哪里出了问题。对于GET和POST路由,命名为的是一个错误。它应该是:

app.get("/todos.json", function (req, res) { //get the data from the collection called 'todos' in MongoDB 

还有:

$.getJSON("todos.json", function(toDoObjects) { 

我应该用todos集合,而不是toDoCollection

"description" : "descTest1", "_id" : ObjectId("58b39a1fb1a30305075408fa"), "tags" : [ "tagTest2" ], "__v" : 0 } 
{ "description" : "descTest2", "_id" : ObjectId("58b4c837d47a5604c7c0609a"), "tags" : [ "tagsTest2" ], "__v" : 0 } 
{ "description" : "descTest3", "_id" : ObjectId("58b4ca0491f4c804d200cda9"), "tags" : [ "tagTest3" ], "__v" : 0 } 
{ "description" : "descTest4", "_id" : ObjectId("58b4e636b71d0a05ebb7a71a"), "tags" : [ "tagTest4" ], "__v" : 0 } 
{ "description" : "descTest5", "_id" : ObjectId("58b60211428520053a4714ed"), "tags" : [ "tagTest5" ], "__v" : 0 } 
{ "_id" : ObjectId("58b6037839e65d96e13cf68e"), "description" : "descTestMONGO", "tags" : [ "tagTestMONGO" ] } 
{ "_id" : ObjectId("58b605b339e65d96e13cf690"), "description" : "Take April to the park", "tags" : [ "chores", "pets" ] }