【问题标题】:Save data not showing up in mongodb using mongoose使用 mongoose 保存未显示在 mongodb 中的数据
【发布时间】:2013-12-25 20:34:48
【问题描述】:

我的图像模式是

 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

新变化 ----->

  { 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 中看不到更新的图像文档。

还有一个单独的文件可以打开 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();

【问题讨论】:

  • 你能正确缩进你的代码吗?
  • 代码的哪一部分不清楚?我会更新的
  • 你解决过这个问题吗?
  • 我确实解决了它......但我不记得如何解决了。抱歉,我在这里没有任何帮助。

标签: node.js mongodb express mongoose


【解决方案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' });

【讨论】:

  • 对于案例 1,我认为 'var Image = require('../models/imageModel'),' 创建一个实例并在应用程序开始时打开与数据库的连接。并且规则 2 也无效,因为我使用非常相似的代码在同一文件中创建文档。
  • 我从来没有看到你的代码有 mongoose.connect(.., ..)。 Require 不会创建连接。
猜你喜欢
  • 1970-01-01
  • 2018-06-11
  • 2020-08-10
  • 2018-06-27
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多