libphonejs - 如何使用它?

时间:2017-05-22 06:06:50

标签: javascript node.js twilio libphonenumber

我有以下尝试发送短信



var number = '**********'

sendText: function(phone, callback) {
        // maybe needs a .then() ?
        var formattedPhone = Phone.format(Phone.parse(phone, 'US'), 'International_plaintext')
        var messageBody = 'test'
        client.sms.messages.create({
            to: formattedPhone,
            from: number,
            body: messageBody
        })
    }, function(error, message) {
        if (error) {
            console.log("SMS ERROR sending to: " + formattedPhone)
            callback(error)
        } else {
            console.log("SMS sent to: " + formattedPhone)
            callback(null, message)
        }
    }




它没有向控制台输出错误或成功字符串 - 是否是Phone.format(Phone.parse())通过阻塞线程或其他东西来调用它?

1 个答案:

答案 0 :(得分:2)

您有语法错误。错误消息的回调函数在大括号之外。

https://github.com/TwilioDevEd/api-snippets/blob/master/rest/messages/send-message/example-1.2.x.js

 sendText: function(phone, callback) {
            // maybe needs a .then() ?
            var formattedPhone = Phone.format(Phone.parse(phone, 'US'), 'International_plaintext')
            var messageBody = 'test';
            client.sms.messages.create({
                to: formattedPhone,
                from: number,
                body: messageBody
            /*})*/ // remove this should be deleted
        }, function(error, message) {
            if (error) {
                console.log("SMS ERROR sending to: " + formattedPhone)
                callback(error)
            } else {
                console.log("SMS sent to: " + formattedPhone)
                callback(null, message)
            }
        });
  }