我使用猫鼬创建了自己的架构,但是该集合没有将传递的数据保存到数据库中,事实上,该集合位于集合的数据库列表中
模型是
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
答案 0 :(得分:0)
我尝试使用与您相同的设置:
这似乎奏效了(得到{ 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" }
这是我的文件。
// 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;
位于public/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>StackOverflow 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>
由于我没有您的完整代码,因此很难确切地知道问题出在哪里:
use movies;
)multipart/form-data
编码,请参见屏幕截图)
https://developer.android.com/training/multiscreen/screensizes 希望这会有所帮助。