2015-09-20 60 views
0

我正在使用一個angularjs前端和nodejs作爲後端,我很困惑爲什麼我的callobj沒有按預期工作。Nodejs全局對象沒有更新

我的相關的NodeJS代碼是:

VAR客戶=要求( 'twilio')(accountSid,的authToken);

var unirest = require('unirest'); 
var express = require("express"); 
var http = require('http'); 
var app = express(); 
var o2x = require('object-to-xml'); 
var qs = require('querystring'); 
app.use(express.static('dist')); 

app.get("/", function (req,res) { 
    res.render('index.html'); 
}); 

var callobj = {}; 

app.post("/call", function(req,res){ 
    callobj.user = req.user; 
    callobj.phone = req.phone; 
    callobj.song = req.song; 
    callobj.mp3link = req.mp3link; 
    console.log(req.body); 
    console.log(callobj); 
    res.send(200); 
}); 

app.get("/call", function(req,res) { 
    console.log(callobj); 
    res.set("Content-Type", "text/xml"); 
    res.send(o2x({ 
     '?xml version="1.0" encoding="utf-8"?' : null, 
     Response: { 
       Play: callobj.mp3link 
     } 
    })); 
}); 

的angularjs代碼:

var app = angular.module('angularexpressApp', []); 

app.controller('appController', function($scope, $http){ 
    $scope.user = 'Shawn'; 
    $scope.phone = '5197227689' 
    $scope.song = 'Cant feel my face'; 
    $scope.mp3link = 'https://api.twilio.com/cowbell.mp3'; 
    $scope.mp3submit = function(){ 
    $http.post('/call', {user: $scope.user, phone: $scope.phone, song: $scope.song, mp3link: $scope.mp3link}) 
     .success(function(data){ 
      //what to do here 
     }) 
     .error(function(data){ 
      console.log('Error: ' + data); 
     }); 
    }; 
}); 
+0

問題是什麼? – SLaks

+0

雅問題不是跟全局對象有關,犯了一個錯誤對不起! – Shawn123

回答

0

它看起來像你分配callobj到錯誤的價值觀。如果我記得正確,它應該設置爲req.body.<param>。請求的主體應該包含你想要的參數。

+0

問題是我需要npm install body parser並在我的快車應用程序中使用它,否則req.body會一直顯示爲undefined。我的錯! – Shawn123

+0

對啊。很酷,祝你好運。 – Eric