[英]Save data not showing up in mongodb using mongoose
My Imageschema is 我的图像格式是
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();
And here is the code for route that saves the image 这是保存图像的路线代码
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);
});
});
});
And the result of console.log is - 并且console.log的结果是-
newchanges.id---> newchanges.id --->
52ba8b0d3ef786bf0d000002
newchanges -----> 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' } ]
image----> 图片---->
{ name: '3519-p2uzci/try1.jpg',
location: '/uploads/3519-p2uzci',
checked: false,
originalname: 'try.zip',
_id: 52ba8b0d3ef786bf0d000002,
__v: 0,
changes: { image: [ [Object] ], changes: [] } }
And I get the updated image document back in response but I cannot see the updated image document in mongodb. 并且我得到更新的图像文档以作为响应,但是我在mongodb中看不到更新的图像文档。
Also there is a separate file that opens mongo db connection and keeps it open in a file database.js 另外还有一个单独的文件,该文件打开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(); module.exports = db();
There are two things that could go wrong: either your document is not saved because you did not connect through mongoose, or you are looking into the wrong place. 有两点可能会出错:您的文档未保存,因为您没有通过猫鼬连接,或者正在寻找错误的位置。
Case 1: did not connect through mongoose http://mongoosejs.com/docs/models.html 情况1:未通过猫鼬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');
Case 2: looking into the wrong place The database that stores the document would be the database you specified when you connected to the database using mongoose. 情况2:查找错误的位置存储文档的数据库将是使用mongoose连接到数据库时指定的数据库。 Since you did not specify a collection name, the rules will be as follows: http://mongoosejs.com/docs/guide.html
由于您未指定集合名称,因此规则如下: 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' });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.