我的Imageschema是
var mongoose = require('mongoose');
var imageModel = function () {
//Define a super simple schema for our products.
var imageSchema = mongoose.Schema({
name: String,
location: String,
checked: Boolean,
changes: {changes: [
{type: String, x1: Number, y1: Number, x2 : Number, y2: Number, comment: String}
],
image: [
{ location: String, x: Number, y: Number ,width: Number, height: Number }
]
},
originalname: String
});
return mongoose.model('Image', imageSchema);
};
module.exports = new imageModel();
这是保存图像的路线代码
var Image = require('../models/imageModel'),
server.post('/updateimage', function(req, res){
var newchanged = req.body.changes ;
var newchanges = req.body;
var newchanges_json = JSON.stringify(newchanged);
console.log(newchanges.id);
console.log(newchanges);
console.log(newchanged);
Image.findOne({_id : newchanges.id}, function(err, image) {
if(err) return res.json({'value': 'failure'});
console.log(image);
for(var i = 0 ; i < newchanges.changes.length; i++){
var temp_data = newchanged[i];
var temp_datajson = JSON.stringify(temp_data);
image.changes.changes[i] = temp_datajson ;
};
// image.checked = true;
image.save(function (err) {
if (err) return console.log(err);
res.send(image);
});
});
});
console.log的结果是 -
newchanges.id ---&GT;
52ba8b0d3ef786bf0d000002
newchanges -----&gt;
{ changes:
[ { type: 'rect', x1: 100, y1: 100, x2: 200, y2: 200, comment: ' ' },
{ type: 'line', x1: 300, y1: 400, x2: 200, y2: 200, comment: ' ' },
{ type: 'circle',
x1: 273.6166687011719,
y1: 372.26666259765625,
x2: 553.6166687011719,
y2: 481.26666259765625 },
{ type: 'circle',
x1: 496.6166687011719,
y1: 244.26666259765625,
x2: 782.6166687011719,
y2: 390.26666259765625 },
{ type: 'circle',
x1: 497.6166687011719,
y1: 204.26666259765625,
x2: 818.6166687011719,
y2: 354.26666259765625 },
{ type: 'circle',
x1: 628.6166687011719,
y1: 176.26666259765625,
x2: 811.6166687011719,
y2: 398.26666259765625 },
{ type: 'circle',
x1: 644.6166687011719,
y1: 244.26666259765625,
x2: 644.6166687011719,
y2: 244.26666259765625 },
{ type: 'circle',
x1: 691.6166687011719,
y1: 163.26666259765625,
x2: 956.6166687011719,
y2: 305.26666259765625 },
{ type: 'circle',
x1: 811.6166687011719,
y1: 228.26666259765625,
x2: 970.6166687011719,
y2: 397.26666259765625 },
{ type: 'circle',
x1: 863.6166687011719,
y1: 307.26666259765625,
x2: 727.6166687011719,
y2: 424.26666259765625,
comment: 'tag' } ],
id: '52ba8b0d3ef786bf0d000002' }
newchanged ---->
[ { type: 'rect', x1: 100, y1: 100, x2: 200, y2: 200, comment: ' ' },
{ type: 'line', x1: 300, y1: 400, x2: 200, y2: 200, comment: ' ' },
{ type: 'circle',
x1: 273.6166687011719,
y1: 372.26666259765625,
x2: 553.6166687011719,
y2: 481.26666259765625 },
{ type: 'circle',
x1: 496.6166687011719,
y1: 244.26666259765625,
x2: 782.6166687011719,
y2: 390.26666259765625 },
{ type: 'circle',
x1: 497.6166687011719,
y1: 204.26666259765625,
x2: 818.6166687011719,
y2: 354.26666259765625 },
{ type: 'circle',
x1: 628.6166687011719,
y1: 176.26666259765625,
x2: 811.6166687011719,
y2: 398.26666259765625 },
{ type: 'circle',
x1: 644.6166687011719,
y1: 244.26666259765625,
x2: 644.6166687011719,
y2: 244.26666259765625 },
{ type: 'circle',
x1: 691.6166687011719,
y1: 163.26666259765625,
x2: 956.6166687011719,
y2: 305.26666259765625 },
{ type: 'circle',
x1: 811.6166687011719,
y1: 228.26666259765625,
x2: 970.6166687011719,
y2: 397.26666259765625 },
{ type: 'circle',
x1: 863.6166687011719,
y1: 307.26666259765625,
x2: 727.6166687011719,
y2: 424.26666259765625,
comment: 'tag' } ]
图像----&GT;
{ name: '3519-p2uzci/try1.jpg',
location: '/uploads/3519-p2uzci',
checked: false,
originalname: 'try.zip',
_id: 52ba8b0d3ef786bf0d000002,
__v: 0,
changes: { image: [ [Object] ], changes: [] } }
我得到了更新后的图像文档作为回应,但我无法在mongodb中看到更新的图像文档。
还有一个单独的文件可以打开mongo db连接并在文件database.js中保持它打开。
'use strict';
var mongoose = require('mongoose');
var db = function () {
return {
config: function (conf) {
mongoose.connect('mongodb://' + conf.host + '/' + conf.database+':27017');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log('db connection open');
});
}
};
};
module.exports = db();
答案 0 :(得分:0)
有两件事可能出错:要么你的文件没有保存,因为你没有通过猫鼬连接,或者你正在寻找错误的地方。
案例1:没有通过mongoose http://mongoosejs.com/docs/models.html
连接Note that no object will be created/removed until the connection your model uses
is open. In this case we are using mongoose.model() so let's open the default
mongoose connection:
mongoose.connect('localhost', 'gettingstarted');
案例2:找错了地方 存储文档的数据库将是您使用mongoose连接到数据库时指定的数据库。由于您未指定集合名称,因此规则如下:http://mongoosejs.com/docs/guide.html
option: collection
Mongoose by default produces a collection name by passing the model name to the
utils.toCollectionName method. This method pluralizes the name. Set this option
if you need a different name for your collection.
var dataSchema = new Schema({..}, { collection: 'data' });