簡體   English   中英

節點應用無法識別功能

[英]Node app doesn't recognize function

因此,我正在編寫一個Node應用程序,並試圖添加一個在創建新用戶時要調用的函數。 我正在使用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.');
    }
  });
}`

但是每次我運行發布請求時,我都會收到一個錯誤

TypeError:twilio.send_sms_to不是函數

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

(這是app.js文件中的“ twilio.send_sms_to(new_user);”行)

...即使Nodemon可以毫無問題地運行服務器。 我究竟做錯了什么?

我的猜測是您只是忘記了在twilio.js文件中導出send_sms_to函數。

嘗試添加module.exports.send_sms_to = send_sms_to; 在其末尾。 否則,當您將其導入其他地方時,他將不會成為該模塊的一部分。

如果我是對的,您可能想在此處了解有關node.js模塊的更多信息。

暫無
暫無

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

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