javascript - var.replace not a function - Stack Overflow

I am getting the error "id.replace is not a function"Below is what I believe the relevant cod

I am getting the error "id.replace is not a function"

Below is what I believe the relevant code is. I am missing something obvious, but my brain is currently mush.

getSyncDbFile: function (config, id) {
    if (id === null)
    {
        .synckolab.tools.logMessage("Error: entry has no id (" +config.name + ": " + config.type + ")", .synckolab.global.LOG_ERROR);
        return null;
    }

    .synckolab.tools.logMessage("syncDbFile:  (" +.synckolab.tools.text.fixNameToMiniCharset(config.serverKey) + "/" + config.type + "_" + config.name + "/" + id + ")", .synckolab.global.LOG_ERROR);

    id = id.replace(/[ :.;$\\\/]\#\@/g, "_");
    var file = Components.classes["@mozilla/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
    try {
        file.append("synckolab");
        if (!file.exists()) {
            file.create(1, parseInt("0775", 8));
        }

        file.append(.synckolab.tools.text.fixNameToMiniCharset(config.serverKey));
        if (!file.exists()) {
            file.create(1, parseInt("0775", 8));
        }


        file.append(config.type + "_" + config.name);
        if (!file.exists()) {
            file.create(1, parseInt("0775", 8));
        }

        file.append(id);
    }
    catch (ex)
    {
        .synckolab.tools.logMessage("Problem with getting syncDbFile:  (" +.synckolab.tools.text.fixNameToMiniCharset(config.serverKey) + "/" + config.name + ": " + config.type + ": " + id + ")\n" + ex, .synckolab.global.LOG_ERROR);
        return null;
    }
    return file;
}

I am getting the error "id.replace is not a function"

Below is what I believe the relevant code is. I am missing something obvious, but my brain is currently mush.

getSyncDbFile: function (config, id) {
    if (id === null)
    {
        .synckolab.tools.logMessage("Error: entry has no id (" +config.name + ": " + config.type + ")", .synckolab.global.LOG_ERROR);
        return null;
    }

    .synckolab.tools.logMessage("syncDbFile:  (" +.synckolab.tools.text.fixNameToMiniCharset(config.serverKey) + "/" + config.type + "_" + config.name + "/" + id + ")", .synckolab.global.LOG_ERROR);

    id = id.replace(/[ :.;$\\\/]\#\@/g, "_");
    var file = Components.classes["@mozilla/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
    try {
        file.append("synckolab");
        if (!file.exists()) {
            file.create(1, parseInt("0775", 8));
        }

        file.append(.synckolab.tools.text.fixNameToMiniCharset(config.serverKey));
        if (!file.exists()) {
            file.create(1, parseInt("0775", 8));
        }


        file.append(config.type + "_" + config.name);
        if (!file.exists()) {
            file.create(1, parseInt("0775", 8));
        }

        file.append(id);
    }
    catch (ex)
    {
        .synckolab.tools.logMessage("Problem with getting syncDbFile:  (" +.synckolab.tools.text.fixNameToMiniCharset(config.serverKey) + "/" + config.name + ": " + config.type + ": " + id + ")\n" + ex, .synckolab.global.LOG_ERROR);
        return null;
    }
    return file;
}
Share Improve this question asked Sep 18, 2012 at 17:03 user1514992user1514992 333 silver badges8 bronze badges 1
  • 1 What is the type of id? it must be a string. – Anoop Commented Sep 18, 2012 at 17:05
Add a ment  | 

3 Answers 3

Reset to default 2

As others have pointed out, id needs to be a string. We have no hint as to what type it is.

Just before the line

id = id.replace(/[ :.;$\\\/]\#\@/g, "_");

Add these two lines:

console.log(id);
console.log(typeof id);

That will let us know what those are and if the right values are being passed.

Have You tried it??

id = String(id).replace(/[ :.;$\\\/]\#\@/g, "_"); 

I think this will work. Since replace is a String Function.

Note: by using this method, it will give a boolean results rather than return a string.

Change :

id.replace(/[ :.;$\\\/]\#\@/g, "_");

to:

(id+"").replace(/[ :.;$\\\/]\#\@/g, "_");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745622532a4636606.html

相关推荐

  • javascript - var.replace not a function - Stack Overflow

    I am getting the error "id.replace is not a function"Below is what I believe the relevant cod

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信