var.replace not a function

user1514992 picture user1514992 · Sep 18, 2012 · Viewed 7.8k times · Source

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)
    {
        com.synckolab.tools.logMessage("Error: entry has no id (" +config.name + ": " + config.type + ")", com.synckolab.global.LOG_ERROR);
        return null;
    }

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

    id = id.replace(/[ :.;$\\\/]\#\@/g, "_");
    var file = Components.classes["@mozilla.org/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(com.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)
    {
        com.synckolab.tools.logMessage("Problem with getting syncDbFile:  (" +com.synckolab.tools.text.fixNameToMiniCharset(config.serverKey) + "/" + config.name + ": " + config.type + ": " + id + ")\n" + ex, com.synckolab.global.LOG_ERROR);
        return null;
    }
    return file;
}

Answer

Jeremy J Starcher picture Jeremy J Starcher · Sep 18, 2012

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.