[英]My mongoose collection not saving the passed data on to the database
I created my schema using mongoose but the collection is not saving the passed data onto the database as a matter of fact the collection is on the database list of collection 我使用mongoose创建了架构,但是集合没有将传递的数据保存到数据库中,事实上,该集合位于集合的数据库列表中
The model is 该模型是
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;
while logistic on saving the documents is 而后勤保存文件是
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
I tried using the same setup as you did: 我尝试使用与您相同的设置:
It seemed to work (got { success: true }
response and stuff stored in the db), as this excerpt from a mongo
console session shows: 它似乎奏效了(得到
{ 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" }
Here are my files. 这是我的文件。
// 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;
Located under public/index.html
位于
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>
Since I do not have your whole code, it's hard to know where the problem lies precisely: 由于我没有您的完整代码,因此很难确切地知道问题出在哪里:
use movies;
) use movies;
) multipart/form-data
encoding, see screenshot) multipart/form-data
编码,请参见屏幕截图) Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.