Моего Imageschema являетсяСохранить данные не отображаются в MongoDB используя мангуст
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 --->
52ba8b0d3ef786bf0d000002
newchanges ----->
{ 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' } ]
изображений ---->
{ name: '3519-p2uzci/try1.jpg',
location: '/uploads/3519-p2uzci',
checked: false,
originalname: 'try.zip',
_id: 52ba8b0d3ef786bf0d000002,
__v: 0,
changes: { image: [ [Object] ], changes: [] } }
И я получаю обновленный документ изображения обратно в ответ, но я не могу увидеть обновленный документ изображения в MongoDB.
Также есть отдельный файл, который открывает соединение Монго дб и держит его открытым в файле 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 = дб();
Не могли бы вы правильно отложить свой код? –
Какая часть кода нечеткая? Я уточню, что – Bhanu423
Вы когда-нибудь решали это? – user2422960