我的猫鼬集合没有将传递的数据保存到数据库中
My mongoose collection not saving the passed data on to the database
我使用 mongoose 创建了我的模式,但该集合并未将传递的数据保存到数据库中,事实上该集合位于集合的数据库列表中
模特是
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MovieSchema = new Schema({
description: String,
category: String,
token: String,
fileID: {
type: Schema.Types.ObjectId,
}
});
const Movie = mongoose.model('Movies', MovieSchema);
module.exports = Movie;
而保存文档的逻辑是
router.post('/', upload.single('file'), (req, res) => {
const movie = new Movie({
description: req.body.Description,
category: req.body.Category,
token: req.body.Description,
fileID: req.file.id
})
movie.save(function(err){
if(err){
console.log(err);
return;
}
res.json({ "success": "true"});
});
});
if i console.log(movie) i can see the objects
我尝试使用与您相同的设置:
- 快递
- 穆尔特
- Multer GridFS 存储
- 猫鼬
它似乎有效(得到 { success: true }
响应和存储在数据库中的内容),正如 mongo
控制台会话的摘录所示:
> db.movies.find();
{ "_id" : ObjectId("5c02a7ccfe06f6644fc891e7"), "fileID" : ObjectId("5c02a7ccfe06f6644fc891d5"), "__v" : 0 }
> db.fs.files.find();
{ "_id" : ObjectId("5c02a7ccfe06f6644fc891d5"), "length" : 4265368, "chunkSize" : 261120, "uploadDate" : ISODate("2018-12-01T15:25:00.411Z"), "filename" : "d32a3c421f8b7bb1654f2abe13e9cf0f", "md5" : "c6203a2cfee5169a8c90015b99bb7844", "contentType" : "image/jpeg" }
这是我的文件。
主要 Express 应用程序文件
// index.js
const express = require('express');
const mongoose = require('mongoose');
const moviesRouter = require('./routes/movies');
mongoose.connect('mongodb://localhost:27017/movies');
const app = express();
app.use(express.static('public'));
app.use('/api/movies', moviesRouter);
app.listen(8000);
电影模特(和你一样)
// models/Movie.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MovieSchema = new Schema({
description: String,
category: String,
token: String,
fileID: {
type: Schema.Types.ObjectId,
}
});
const Movie = mongoose.model('Movies', MovieSchema);
module.exports = Movie;
电影路线(大部分也是您的代码)
// routes/movies.js
const express = require('express');
const multer = require('multer');
const router = express.Router();
const Movie = require('../models/Movie');
// Create a storage object with a given configuration
const storage = require('multer-gridfs-storage')({
url: 'mongodb://localhost:27017/movies'
});
// Set multer storage engine to the newly created object
const upload = multer({ storage: storage });
router.post('/', upload.single('file'), (req, res) => {
const movie = new Movie({
description: req.body.Description,
category: req.body.Category,
token: req.body.Description,
fileID: req.file.id
});
movie.save(function(err){
if(err){
console.log(err);
return;
}
res.json({ "success": "true"});
});
});
module.exports = router;
HTML 测试页
位于public/index.html
下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Whosebug mongoose/gridfs question</title>
</head>
<body>
<div id="status"></div>
<form id="movie" method="POST" enctype="multipart/form-data">
<label for="file">Choose movie</label>
<input id="file" type="file" name="file" />
<input type="submit" value="Send" />
</form>
<script>
const status = document.getElementById('status');
const form = document.getElementById('movie');
const fileInput = document.getElementById('file');
console.log(fileInput);
form.addEventListener('submit', event => {
event.preventDefault();
const formData = new FormData();
formData.append('file', fileInput.files[0]);
var request = new XMLHttpRequest();
request.open('POST', '/api/movies');
request.onload = function(event) {
if (request.status == 200) {
status.innerHTML = 'Sent!';
} else {
status.innerHTML = `Error: ${request.status}`;
}
};
request.send(formData);
});
</script>
</body>
</html>
由于我没有你的完整代码,所以很难准确知道问题出在哪里:
- 您的 mongo 数据库设置好了吗? (例如
use movies;
)
- 当你将数据发送到服务器时,数据编码方式是否有问题(你应该有
multipart/form-data
编码,见截图)
希望这对您有所帮助。
我使用 mongoose 创建了我的模式,但该集合并未将传递的数据保存到数据库中,事实上该集合位于集合的数据库列表中
模特是
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MovieSchema = new Schema({
description: String,
category: String,
token: String,
fileID: {
type: Schema.Types.ObjectId,
}
});
const Movie = mongoose.model('Movies', MovieSchema);
module.exports = Movie;
而保存文档的逻辑是
router.post('/', upload.single('file'), (req, res) => {
const movie = new Movie({
description: req.body.Description,
category: req.body.Category,
token: req.body.Description,
fileID: req.file.id
})
movie.save(function(err){
if(err){
console.log(err);
return;
}
res.json({ "success": "true"});
});
});
if i console.log(movie) i can see the objects
我尝试使用与您相同的设置:
- 快递
- 穆尔特
- Multer GridFS 存储
- 猫鼬
它似乎有效(得到 { success: true }
响应和存储在数据库中的内容),正如 mongo
控制台会话的摘录所示:
> db.movies.find();
{ "_id" : ObjectId("5c02a7ccfe06f6644fc891e7"), "fileID" : ObjectId("5c02a7ccfe06f6644fc891d5"), "__v" : 0 }
> db.fs.files.find();
{ "_id" : ObjectId("5c02a7ccfe06f6644fc891d5"), "length" : 4265368, "chunkSize" : 261120, "uploadDate" : ISODate("2018-12-01T15:25:00.411Z"), "filename" : "d32a3c421f8b7bb1654f2abe13e9cf0f", "md5" : "c6203a2cfee5169a8c90015b99bb7844", "contentType" : "image/jpeg" }
这是我的文件。
主要 Express 应用程序文件
// index.js
const express = require('express');
const mongoose = require('mongoose');
const moviesRouter = require('./routes/movies');
mongoose.connect('mongodb://localhost:27017/movies');
const app = express();
app.use(express.static('public'));
app.use('/api/movies', moviesRouter);
app.listen(8000);
电影模特(和你一样)
// models/Movie.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MovieSchema = new Schema({
description: String,
category: String,
token: String,
fileID: {
type: Schema.Types.ObjectId,
}
});
const Movie = mongoose.model('Movies', MovieSchema);
module.exports = Movie;
电影路线(大部分也是您的代码)
// routes/movies.js
const express = require('express');
const multer = require('multer');
const router = express.Router();
const Movie = require('../models/Movie');
// Create a storage object with a given configuration
const storage = require('multer-gridfs-storage')({
url: 'mongodb://localhost:27017/movies'
});
// Set multer storage engine to the newly created object
const upload = multer({ storage: storage });
router.post('/', upload.single('file'), (req, res) => {
const movie = new Movie({
description: req.body.Description,
category: req.body.Category,
token: req.body.Description,
fileID: req.file.id
});
movie.save(function(err){
if(err){
console.log(err);
return;
}
res.json({ "success": "true"});
});
});
module.exports = router;
HTML 测试页
位于public/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Whosebug mongoose/gridfs question</title>
</head>
<body>
<div id="status"></div>
<form id="movie" method="POST" enctype="multipart/form-data">
<label for="file">Choose movie</label>
<input id="file" type="file" name="file" />
<input type="submit" value="Send" />
</form>
<script>
const status = document.getElementById('status');
const form = document.getElementById('movie');
const fileInput = document.getElementById('file');
console.log(fileInput);
form.addEventListener('submit', event => {
event.preventDefault();
const formData = new FormData();
formData.append('file', fileInput.files[0]);
var request = new XMLHttpRequest();
request.open('POST', '/api/movies');
request.onload = function(event) {
if (request.status == 200) {
status.innerHTML = 'Sent!';
} else {
status.innerHTML = `Error: ${request.status}`;
}
};
request.send(formData);
});
</script>
</body>
</html>
由于我没有你的完整代码,所以很难准确知道问题出在哪里:
- 您的 mongo 数据库设置好了吗? (例如
use movies;
) - 当你将数据发送到服务器时,数据编码方式是否有问题(你应该有
multipart/form-data
编码,见截图)
希望这对您有所帮助。