【发布时间】:2017-07-18 19:48:29
【问题描述】:
我是使用 MongoDB 的新手。目前,我正在制作一个需要一些数据存储的 Web 应用程序。我在 Node.js 上建立了一个 HTTP 服务器,它在 localhost:3000 上运行。我还使用 Vagrant 和 VirtualBox 构建了一个虚拟开发环境。我正在从 PuTTy 访问 Mongo shell(如果这完全相关的话)。在合并 MongoDB 之前,它运行良好,因为我将数据存储在 Node.js 程序内存中。
此网络应用程序是一个在线待办事项列表。我遇到的第一个错误是 get 路线。我已插入 Mongo 数据库的“待办事项”列表不会出现在 localhost 的站点上。它似乎没有从数据库中获取数据。我得到的第二个错误是 post 路线。当我通过本地主机上的用户界面插入“待办事项”并刷新页面时,本地主机上的待办事项列表会更新为特定的待办事项(但不是我插入数据库中的待办事项)。但是,它似乎没有将其添加到数据库中,并且我仍然在控制台上收到此错误:
vagrant@precise32:~/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);
})
});
mongo shell
> 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");
解决了 promise 错误的问题。不过记得先安装模块:
npm install --save bluebird
【问题讨论】:
-
当您从 UI 尝试 /post 方法时,您可以通过命令行使用 db.toDoCollection.find() 查看结果吗?
-
当我从 UI 执行 post 方法时,toDoCollection 没有得到更新。我输入 db.toDoCollection.find();但列出的唯一数据是我自己通过外壳插入数据库的数据。但是,由于某种原因,当我刷新时,UI 会使用新数据进行更新。
-
我找到了一个可以回答这个问题的问题。在您需要 mongoose 之后,立即调用 'mongoose.Promise = global.Promise;'或导入'bluebird'节点包并调用'mongoose.Promise = require('bluebird');'在您建立联系之前。我已将此标记为重复。这是原始答案stackoverflow.com/questions/38138445/…的链接
-
那也不行。
标签: javascript node.js mongodb mongoose database