2013-10-20 101 views
0

我不太確定我要出錯的地方,因爲我現在試着調試一下。我正在創建一個webapp的開始部分。現在,我正在嘗試使用主幹來保存表單。我正在使用Node.js,Mongoose,Express和Backbone。保存骨幹模型導致ajax調用只是掛起

除了當我將模型發送回服務器時,一切似乎都正常工作,請求掛起並最終導致超時。下面是我所看到的屏幕截圖:I tried the request twice and both time, it's hangs and then times out

我不太清楚問題所在,因爲我仍然是這種堆棧的新手。

這是我的Backbone視圖。沒什麼特別的。它呈現使用灰塵的形式。在submit方法中,我將所有表單的數據收集到一個數組中,並形成一個新的用戶模型,然後將其保存回服務器。同樣,服務器IS將數據保存在數據庫中。然而,當我看到在Firebug,我看到POST請求只是掛在那兒,並沒有結束(它具有紡紗裝東西,如果它仍然在等待服務器的響應。

define(['dust/addUser','models/userModel'], function(AddUser, UserModel) { 
    AddUser = Backbone.View.extend({ 
     initialize: function() { 
      this.render(); 
     }, 
     tagName: 'div', 
     id: 'addUserContainer', 
     events: { 
      'submit form': 'submit' 
     }, 
     render: function() { 
      $thisSelf = this.$el; 
      dust.render('addUser','', function(error, output) { 
       $thisSelf.append(output); 
      }); 

      $('#wrapper').append($thisSelf); 
     }, 
     /** 
     * @method submit 
     * Capture the submit event, create a logical array of data, send to model 
     */ 
     submit: function(event) { 
      event.preventDefault(); 

      var dataArray = $('#addUser').serializeArray(), 
        data = _(dataArray).reduce(function(data, field) { 
         data[field.name] = field.value; 
         return data; 
        }, {}); 

        var newUserModel = new UserModel(data); 
        console.log(newUserModel); 
        newUserModel.save({ 
         success: function(model, response) { 
          console.log(model); 
          console.log(response); 
         }, 
         error: function(model, response) { 
          console.log(model); 
          console.log(response); 
         } 
        }); 
     }, 
    }); 

這裏有。相關部分我server.js文件在這裏,我需要貓鼬和創建連接

var application_root = __dirname, 
express = require('express'), 
path = require('path'), 
mongoose = require('mongoose'), 
app = express(); 

//database 
mongoose.connect('mongodb://nodejitsu:[email protected]:10041/nodejitsudb7458111315'); 

這裏是我創建的API路線:

app.post('/api/users', function(req, res) { 
    var user; 
    user = new UserModel({ 
    rider: req.body.rider, 
    driver: req.body.driver, 
    name: req.body.name, 
    email: req.body.email, 
    phone: req.body.phone, 
    address: req.body.address, 
    city: req.body.city, 
    state: req.body.state, 
    zip: req.body.zip, 
    startTime: req.body.startTime, 
    endTime: req.body.endTime, 
    officeLocation: req.body.officeLocation 
    }); 
    user.save(function(err) { 
    if (!err) { 
     return console.log("user created."); 
    } else { 
     return console.log(err); 
    } 
    }); 
}); 

任何指針將選取gre非常感謝。 :)

回答

0

我想通了。我沒有回覆成功的迴應。之前我曾嘗試過這樣做時,我並沒有意識到我也已經返回了一些東西。

這裏是我的貓鼬更新代碼:

app.post('/api/users', function(req, res) { 
    var user; 
    user = new UserModel({ 
    rider: req.body.rider, 
    driver: req.body.driver, 
    name: req.body.name, 
    email: req.body.email, 
    phone: req.body.phone, 
    address: req.body.address, 
    city: req.body.city, 
    state: req.body.state, 
    zip: req.body.zip, 
    startTime: req.body.startTime, 
    endTime: req.body.endTime, 
    officeLocation: req.body.officeLocation 
    }); 
    user.save(function(err) { 
    if (!err) { 
     return res.send(200, 'User saved.'); 
    } else { 
     return res.send(500) 
    } 
    }); 
});