我正在尝试将图像和自动增量变量插入mongodb,但数据没有插入。我的代码是
var express = require('express');
var fs = require('fs');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var multer = require('multer');
var app=express();
var autoIncrement = require("mongoose-auto-increment");
var MongoClient = require('mongodb').MongoClient;
var connection=mongoose.createConnection('mongodb://localhost/db');
autoIncrement.initialize(connection);
var schema1 = new Schema(
{ img:{ data: Buffer, contentType: String },
chieldId:{type:Number,autoincrement:true}
}
);
schema1.plugin(autoIncrement.plugin,{model:'carousel2',field:'chieldId'});
var Item = mongoose.model('carousel2',schema1);
app.use(multer({ dest: './uploads/'}).array('photo'));
app.post('/upload',function(req,res){
var len=req.files.length;
function repeater(i){
if(i<len){
var newItem = new Item();
newItem.img.data = fs.readFileSync(req.files[i].path);
newItem.img.contentType = 'image/jpg';
newItem.save();
// console.log(chieldId);
repeater(i+1);
}
}
repeater(0);
res.send('Successfully uploaded!');
});
app.get('/', function (req, res, next) {
res.writeHead(200, {'Content-Type': 'text/html'});
var form = '<form action="/upload" enctype="multipart/form-data" method="post"><br><input multiple="multiple" name="photo" type="file" /><br><br><input type="submit" value="Upload" /></form>';
res.end(form);
});
app.listen(8080);
当我运行此代码时,没有错误并且我正在上传消息,但是当我打开mongo shell并尝试查看插入的数据时,它不会返回任何值。这是一个新手,任何帮助,不胜感激< / p>