2016-04-24 5 views
0

ノードアプリケーションを作成しています。新しいユーザーを作成するときに呼び出す関数を追加しようとしています。私はTwilio APIを使って、ユーザーがアプリにサインアップするとSMSメッセージを誰かに送信しています。ノードアプリケーションで関数が認識されない

app.js

var express = require('express'); 
var db = require('./models'); 
var bodyParser = require('body-parser'); 
var methodOverride = require("method-override"); 
var app = express(); 
var session = require('express-session'); 
var app = express(); 
var twilio = require('./js/twilio.js'); 

// app.set('port', (process.env.PORT || 3000)); 

app.use(express.static(__dirname + '/public')); 

// views is directory for all template files 
app.set('views', __dirname + '/views'); 
app.post("/signup", function (req, res) { 
    // Creating object to pass into db and twilio: 
    var new_user = { 
    first_name : req.body.first_name, 
    last_name : req.body.last_name, 
    phone : req.body.phone, 
    partner_phone : req.body.partner_phone, 
    email : req.body.email, 
    password : req.body.password 
    }; 
console.log("The new_user is: ", new_user) 
    twilio.send_sms_to(new_user); 
    db.User.create({first_name: new_user.first_name, last_name: new_user.last_name,  phone: new_user.phone, partner_phone: new_user.partner_phone, email:  new_user.email, password: new_user.password}).then(function(user){ 
     res.render("login"); 
    }); 
}); 

twilio.js

var send_sms_to = function (user) { 
var client = new twilio.RestClient('secret key', 'super secret key'), msg =  "Hello potential new user!", 
    phone = user.partner_phone, 
    name = user.first_name + " " + user.last_name; 
    console.log("Entries are: ", user.phone, user.first_name) 
    client.sms.messages.create({ 
    to: phone, 
    from:'+16507970229', 
    body: msg 
}, function(error, message) { 
    // The HTTP request to Twilio will run asynchronously. This callback 
    // function will be called when a response is received from Twilio 
    // The "error" variable will contain error information, if any. 
    // If the request was successful, this value will be "falsy" 
    if (!error) { 
     // The second argument to the callback will contain the information 
     // sent back by Twilio for the request. In this case, it is the 
     // information about the text messsage you just sent: 
     console.log('Success! The SID for this SMS message is:'); 
     console.log(message.sid); 
     console.log('Message sent on:'); 
     console.log(message.dateCreated); 
    } else { 
     console.log('Oops! There was an error.'); 
    } 
    }); 
}` 

しかし、私はPOSTリクエストを実行するたびに、私はエラーに

TypeError例外を取得:twilio.send_sms_to機能

ではありません

at /Users/jamesbradley/codeProjects/indulge/indulge_app/app.js:256:10

(これはライン "twilio.send_sms_to(new_user);" であるapp.jsファイルに)

... Nodemonは問題なくサーバーを実行していても。私は間違って何をしていますか?

+0

サーバー上の単純なパスの問題のようです。どうやら、require( './ public/js/twilio.js');はあなたの 'twilio.js'への正しいパスではありません。 – jfriend00

+0

'app.js'の' twilio.js'へのパスとエラー出力が一致せず、先頭のピリオド( '.')が欠けています。エラーの原因となるかもしれないtwilio.jsが必要な場所はありますか? –

+0

すべてを試しましたが、パスを修正する方法がわかりません: ( './public/js/twilio.js') ( './js/twilio.js') ( '../ ( 'public/js/twilio.js') ( '../js/twilio.js') ( 'public/js/twilio.js') ( 'js/twilio.js') –

答えて

1

関数をtwilio.jsファイルに書き出すことを忘れてしまったと思います。

最後にmodule.exports.send_sms_to = send_sms_to;を追加してみてください。それ以外の場合は、他の場所にインポートするときにモジュールの一部になりません。

私が正しいとすれば、node.jsモジュールhereについてもっと知りたいかもしれません。

関連する問題