【问题标题】:Unable to save mongodb record via node script using mongoose无法使用 mongoose 通过节点脚本保存 mongodb 记录
【发布时间】:2012-04-25 14:59:24
【问题描述】:

这是我的 js 文件,描述了不起作用的架构和 API。当我通过命令行工具执行此操作时...架构非常简单,我已经实现了一些简单的查找命令。

'use strict'

var util    = require('util');
var bcrypt  = require('bcrypt');
var mongoose = require('mongoose');
var Schema   = mongoose.Schema;

var validatePresenceOf = function(value){
  return value && value.length; 
};

var toLower = function(string){
  return string.toLowerCase();
};

var SportsStandings = new Schema({
  'sport' : { type : String, 
              validate : [validatePresenceOf, 'a sport is required'],
              set : toLower
            },
  'league' : { type : String, 
              validate : [validatePresenceOf, 'a league is required'],
              set : toLower
            },
  'division' : { type : String, 
              validate : [validatePresenceOf, 'a division is required'],
              set : toLower
            },
  'teamName' : { type : String, 
              validate : [validatePresenceOf, 'a teamName is required'],
              set : toLower
            },
   'wins' : { type : Number, min: 0, 
              validate : [validatePresenceOf, 'wins is required'],
            },
   'losses' : { type : Number, min: 0, 
              validate : [validatePresenceOf, 'losses is required'],
            }
});

SportsStandings.statics.findTeamRecord = function(sport, league, 
                                                  division, teamName,
                                              cb) {
  return  this.find({'sport' : sport, 'league' : league, 
                     'division' : division, 'teamName': teamName}, cb);
};

SportsStandings.statics.findBySport = function(sport, cb) {
  return  this.find({'sport' : sport}, cb);
};

module.exports = mongoose.model('SportsStanding' , SportsStandings);

这是一个简单的节点脚本,它实例化上面导出的对象并尝试在模型上执行保存命令.....

'use strict'

var util = require('util');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/mydb');
var SportsStanding  = require('../schemas/SportsStandings');

var record = new SportsStanding({ 
          'sport' : 'mlb',
          'league' : 'AL',
          'divison' : 'east',
          'teamName' : 'New York Yankees',
          'wins' : 10,
          'losses' : 1});

record.save(function(err) {
    console.log('error: ' + err);
    SportsStandings.find().all(function(arr) {
    console.log(arr); 
    console.log('length='+arr.length);
    });
});

process.exit();

【问题讨论】:

  • 您看到了什么错误?当您说它不起作用时,是否有一些输出可以帮助我们诊断问题?
  • 什么也没发生。我在命令行上使用节点运行程序:nodesportsStandings.js,据我所知,使用 mongo 命令行工具没有错误,也没有向数据库提交任何内容。如果我使用 mongo 命令行执行 db.sportsStandings.insert(....) 它可以工作....

标签: node.js mongodb mongoose


【解决方案1】:

请记住,在使用 node.js 进行编程时,您必须非常小心事件驱动的编程风格。您的代码的问题似乎是您在外部执行级别调用process.exit()。当您调用 record.save(...) 时,它会立即将控制权返回到该外部执行级别,并且不允许执行保存或保存回调中的任何代码。

要解决此问题,您需要将 process.exit() 移动到最内层回调函数的末尾,然后您应该会看到预期的结果。

运行您的示例,我发现了其他几个需要纠正的错别字和错误。检查SportStanding(s) 模型变量的拼写并确保它在任何地方都匹配。此外,模型上的 find() 需要回调,它返回数据库中的所有记录(作为第二个参数 - 错误标志是第一个),因此不需要链接的 all() 调用。你想要的保存功能最终应该是这样的:

record.save(function(err) {
  console.log('error: ' + err);
  SportsStandings.find(function(err, arr) {
      console.log(arr);
      console.log('length='+arr.length);
      process.exit();
  });
});

【讨论】:

  • 非常感谢,已解决。
猜你喜欢
  • 1970-01-01
  • 2014-07-07
  • 2020-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 2018-06-15
  • 2015-08-25
相关资源
最近更新 更多