【问题标题】:call a function from another file and get a random result for each call从另一个文件调用函数并为每次调用获取随机结果
【发布时间】:2018-09-15 18:26:09
【问题描述】:

我有一个包含这个的 random.js 文件来获取随机假 IP

exports.ip = function () {
    let random = (Math.floor(Math.random() * 255) + 1)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0); 
    return random
}

我调用 send.js 文件中的变量来替换字符串 {randomip}

let replace_tag = function (to) {

    config.message.subject = config.message.subject
        .replace("{randomip}", random.ip)
        .replace("{email}", to)
        .replace("{date}", random.date);

    config.message.fromname = config.message.fromname
        .replace("{randomip}", random.ip)
        .replace("{email}", to)
        .replace("{date}", random.date);

    config.message.fromemail = config.message.fromemail
        .replace("{randomip}", random.ip)
        .replace("{email}", to)
        .replace("{date}", random.date);

}

但是它只会产生一个生成的ip,我想让它在每次调用时随机生成会产生不同的值

我尝试将其插入循环中,但仍然无法正常工作

我在另一个函数中调用替换函数,然后像这样进入循环

let kirim = function (to) {

    replace_tag(to);

    let message = {
        from: config.message.fromname+'<'+config.message.fromemail+'>',
        to: to,
        subject: config.message.subject,
        text: config.message.text,
        html: html
    };

    transporter.sendMail(message, (error, info) => {
        if (error) {
             return console.log(error.response)
        }
        console.log('Message sent: ',info.accepted);
    });
};


(async () => {

    for (var i in list) {

        kirim(list[i]);
        await delay(config.send.delay*1000); 

    }

})();

【问题讨论】:

  • 您可以改为导出函数
  • 你导出了一个值而不是一个生成值的函数
  • 我已经将它编辑为一个函数,但它仍然不是每次调用都是随机的
  • 你叫它random.ip()而不是random.ip
  • 我调用 random.ip 因为我将它作为对象导入

标签: javascript node.js function random call


【解决方案1】:

我在另一个函数中调用替换函数然后进入循环

啊,你的问题。您的replace_tag 函数将更改config 对象,并且在第一次调用后它不再包含模板标签,而是包含替换结果。对replace_tag 的进一步调用将不再在您的配置中找到{randomip},因此不会生成新的ip。

您应该保持配置不变(不可变),并在每次需要时创建新的消息对象。然后,这些对象中的每一个都将具有不同的随机 IP 地址。

// takes a string, returns a new string
function replace_tags(input, email) {
    return input
    .replace("{randomip}", random.ip)
    .replace("{email}", email)
    .replace("{date}", random.date);
}
// returns a new object, leaves config.message unaltered
function get_customised_message_template(to) {
    return {
        subject: replace_tags(config.message.subject, to),
        fromname: replace_tags(config.message.fromname, to),
        fromemail: replace_tags(config.message.fromemail, to),
    };
}

function kirim(to) {
    const random_message = get_customised_message_template(to);
//  ^^^^^^^^^^^^^^^^^^^^^^
    const message = {
        from: random_message.fromname+'<'+random_message.fromemail+'>',
        to: to,
        subject: random_message.subject,
        text: config.message.text,
        html: html
    };

    transporter.sendMail(message, (error, info) => {
        if (error) console.log(error.response);
        else console.log('Message sent: ', info.accepted);
    });
};

【讨论】:

    【解决方案2】:

    我认为它会起作用。

    random.js:

    exports.ip = function() {
        return (Math.floor(Math.random() * 255) + 1)+"."+(Math.floor(Math.random() * 255) + 
            0)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0);
    }
    

    send.js:

    let replace_tag = function (to) {
    
        config.message.subject = config.message.subject
            .replace("{randomip}", random.ip())
            .replace("{email}", to)
            .replace("{date}", random.date);
    
        config.message.fromname = config.message.fromname
            .replace("{randomip}", random.ip())
            .replace("{email}", to)
            .replace("{date}", random.date);
    
        config.message.fromemail = config.message.fromemail
            .replace("{randomip}", random.ip())
            .replace("{email}", to)
            .replace("{date}", random.date);
    
    }
    

    【讨论】:

    • 尝试创建一个生成随机ip的函数,例如:function getRandomIp() { return (Math.floor(Math.random() * 255) + 1)+"."+(Math .floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0);并在你想生成随机ip的地方调用它
    • 是的,它和我的函数一样,但是当我将它导出到另一个文件时,它不能是随机的
    【解决方案3】:
    function getRandomIp() {
        return (Math.floor(Math.random() * 255) + 1)+"."+(Math.floor(Math.random() * 255) + 
        0)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0);
    }
    let replace_tag = function (to) {
    
        config.message.subject = config.message.subject
            .replace("{randomip}", getRandomIp())
            .replace("{email}", to)
            .replace("{date}", random.date);
    
        config.message.fromname = config.message.fromname
            .replace("{randomip}", getRandomIp())
            .replace("{email}", to)
            .replace("{date}", random.date);
    
        config.message.fromemail = config.message.fromemail
            .replace("{randomip}", getRandomIp())
            .replace("{email}", to)
            .replace("{date}", random.date);
    
    }
    

    【讨论】:

    • 如果我将函数导出到另一个文件并像对象一样调用它会怎样
    • 我认为你可以
    • 您是如何链接文件的?
    • 使用 const random = require('./random.js');
    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2018-05-15
    • 2011-03-27
    • 1970-01-01
    相关资源
    最近更新 更多