2016-04-24 55 views
0

因此,我正在编写一个Node应用程序,并且我试图添加一个函数,以在创建新用户时调用。我使用Twilio API来为用户注册应用程序后向其发送短信。节点应用程序无法识别功能

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.'); 
    } 
    }); 
}` 

但我每次运行发布请求时,我得到一个错误

类型错误: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'的地方可能会导致错误? ('./js/twilio.js') ('../ –

+0

我尝试了一切,但无法弄清楚如何修复路径: ('./public/js/twilio.js') ('./js/twilio.js') ('/ js/twilio.js') ('../js/twilio.js') ('public/js/twilio.js') ('js/twilio.js') –

回答

1

我的猜测是你只是忘记了在你的twilio.js文件中输出send_sms_to函数。

尝试在其末尾添加module.exports.send_sms_to = send_sms_to;。否则,当你将它导入其他地方时,他不会成为模块的一部分。

如果我是对的,你可能想了解更多关于node.js模块here