我正在關注Jose Annunziato的YouTube教程。我創建了我的server.js
併爲我的數據庫連接進行了所有必需的設置和配置。現在,當我從表單向服務器發佈某些內容時:它顯示數據已成功發送到服務器,但是當我到mongo控制檯來驗證數據是否已收到並且數據庫是否已創建時。我運行db
它說測試我運行show dbs
,在那裏我看不到我的新數據庫。我不確定實際問題是什麼,因爲我在教程中做了Jose所說的一切。mongodb:在插入數據後,數據庫沒有顯示在show dbs中
Server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/blogfall2016');
var PostSchema = mongoose.Schema({
title: {type: String, required: true},
body: String,
tag: {type: String, enum:['POLITICS', 'ECONOMY', 'EDUCATION']},
posted: {type: Date, default: Date.now},
});
var PostModel = mongoose.model('PostModel', PostSchema)
// GET /style.css etc
app.use(express.static(__dirname + '/public'));
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/api/blogpost", CreatePost);
function CreatePost(req, res) {
var post = req.body;
console.log(post);
PostModel.create(post);
res.json(post);
}
app.listen(3000);
url是什麼意思? – Sami
var url ='mongodb:// localhost/blogfall2016'; –
http://take.ms/AGodw – Sami