Node.js 使用mongoose保存mongodb中未显示的数据

Node.js 使用mongoose保存mongodb中未显示的数据,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我的图像模式是 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: [

我的图像模式是

 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);
  });
      });
     });
新变化------>

图像-->

我得到了更新后的图像文档作为响应,但在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()

有两件事可能会出错:要么您的文档没有保存,因为您没有通过mongoose进行连接,要么您正在查看错误的位置

案例1:未通过猫鼬连接

案例2:寻找错误的地方 存储文档的数据库将是使用mongoose连接到数据库时指定的数据库。由于未指定集合名称,因此规则如下所示:


你能正确缩进你的代码吗?代码的哪一部分不清楚?我会更新你解决过这个问题吗?我解决过。。。但我不记得是怎么回事。很抱歉,我在这里没有任何帮助。对于案例1,我认为“var Image=require(“../models/imageModel”),“将创建一个实例,并在应用程序启动时打开与数据库的连接。规则2也是无效的,因为我正在使用一个非常相似的代码在同一个文件中创建文档。我从来没有看到您的代码有mongoose.connect(…..)。Require不创建连接。
  { 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: [] } }
 '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');
        });
     }
    }; 
   };
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');
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' });