簡體   English   中英

從node.js應用程序調用時,Mongoose Model.save()會掛起

[英]Mongoose Model.save() hangs when called from node.js app

我正在嘗試學習node和mongo,以便構建一個簡單的Web應用程序/自學一些關於Web應用程序的內容。 但是,當我調用Model.save()時,似乎永遠不會執行continuation函數,並且不會保存數據。

這是我到目前為止所擁有的:

/* app.js */

var express = require('express')
  , app = express()
  , routes = require('./routes')
  , http = require('http')
  , path = require('path')
  , mongoose = require('mongoose')
  , db 
  , Track
  , models = require('./models.js');

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('secretstuff'));
  app.use(express.session());
  app.use(app.router);
  app.use(require('less-middleware')({ src: __dirname + '/public' }));
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(function(err, req, res, next){
    console.error(err.stack);
    res.send(500, 'Something broke!');
  });
});

models.defineModels(mongoose, function(){
  app.Track = Track = mongoose.model('Track');
  db = mongoose.createConnection('localhost','nextrak')
});

app.get('/', routes.index);

app.get('/dbTest', function(req, res){
  console.log("Here goes...");
  var t = new Track({
    name: "TestTrack",
    artist: "Artist",
    tags: ["test"],
    next: ["track1","track2"]
  });
  console.log("Test Track:");
  console.log(t);

  t.save(function(){
    if(err)
      console.log("Error! Couldn't complete dbTest successfully.");
    else
      console.log("Aw yiss, got dbTest working");
  })
});

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});


/*models.js*/

function defineModels(mongoose, cont){
  var Schema = mongoose.Schema;
  Track = new Schema({
    'name': { type: String, index: true },
    'artist': String,
    'tags': [String],
    'next': [String],
  });
  mongoose.model('Track', Track);
  cont();
}

exports.defineModels = defineModels;

不會拋出任何錯誤,並且mongo日志表明我啟動應用程序時會啟動5個新連接。 不顯示新日志([clientcursormon]日志除外)。 當我在Chrome中加載/ dbTest時,應用程序打印出以下內容:

開始...

  Test Track:
    { name: 'TestTrack',
      artist: 'Basik',
      _id: 5031606aa11cf95815000001,
      next: [ 'track1', 'track2' ],
      tags: [ 'test' ] }

Mongo似乎配置正確。 當我有節點運行Mongoose引導您完成的簡單“入門”腳本時,一切正常。

誰能指出我做錯了什么?

您尚未為Mongoose創建默認使用的連接。 替換這個:

db = mongoose.createConnection('localhost','nextrak')

有了這個:

db = mongoose.connect('localhost', 'nextrak');

結合其他尼特:

  1. 您將Track設置為models.js中的全局變量
  2. 您需要在t.save回調中添加一個err參數。

首先,如果您只需要一個連接,則應使用mongoose.connect()。

其次,我認為你正在混合軌道模式和跟蹤模型。 這是兩件不同的事情。 new Schema()創建一個Schema對象,該對象將傳遞給mongoose.model()。 mongoose.model(它似乎你要丟棄)的結果是你想要在創建一個新實例存儲在數據庫而不是模式時使用的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM