{"record":{"id":"68857950a5eb6207","repo":"jashkenas/backbone","slug":"a-url-property-or-function-must-be-specified","errorCode":null,"errorMessage":"A \"url\" property or function must be specified","messagePattern":"A \"url\" property or function must be specified","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"backbone.js","lineNumber":2143,"sourceCode":"\n    // Set the prototype chain to inherit from `parent`, without calling\n    // `parent`'s constructor function and add the prototype properties.\n    child.prototype = _.create(parent.prototype, protoProps);\n    child.prototype.constructor = child;\n\n    // Set a convenience property in case the parent's prototype is needed\n    // later.\n    child.__super__ = parent.prototype;\n\n    return child;\n  };\n\n  // Set up inheritance for the model, collection, router, view and history.\n  Model.extend = Collection.extend = Router.extend = View.extend = History.extend = extend;\n\n  // Throw an error when a URL is needed, and none is supplied.\n  var urlError = function() {\n    throw new Error('A \"url\" property or function must be specified');\n  };\n\n  // Wrap an optional error callback with a fallback error event.\n  var wrapError = function(model, options) {\n    var error = options.error;\n    options.error = function(resp) {\n      if (error) error.call(options.context, model, resp, options);\n      model.trigger('error', model, resp, options);\n    };\n  };\n\n  // Provide useful information when things go wrong. This method is not meant\n  // to be used directly; it merely provides the necessary introspection for the\n  // external `debugInfo` function.\n  Backbone._debug = function() {\n    return {root: root, _: _};\n  };\n","sourceCodeStart":2125,"sourceCodeEnd":2161,"githubUrl":"https://github.com/jashkenas/backbone/blob/f229c75b194a63a8e07a3313ce43e8d2dc105327/backbone.js#L2125-L2161","documentation":"urlError is Backbone's fallback thrown by Model/Collection sync when a fetch/save/destroy/sync call needs a URL but neither a 'url' nor 'urlRoot' property (or function) is defined. Backbone refuses to build a request it cannot address, so it throws synchronously. It's a configuration error on the model/collection, not a network failure.","triggerScenarios":"Calling model.fetch()/save()/destroy() on a Model with no url or urlRoot and that is not part of a collection with a url; calling collection.fetch() on a Collection without a url; calling Backbone.sync directly with options.url undefined for an unsaved, collection-less model.","commonSituations":"Forgetting to define url/urlRoot on a standalone model (especially one constructed without a collection); renaming/refactoring the url property; defining url on the instance after construction but after calling fetch; subclassing Model and dropping the url config from the parent; typo like 'urls' instead of 'url'.","solutions":["Define urlRoot on the model: 'urlRoot: \"/api/users\"' (fetch/save/destroy will hit /api/users/<id>).","Define a url function for dynamic or nested URLs: 'url: function(){ return \"/api/users/\" + this.id; }'.","Pass an explicit url in options: model.fetch({url: \"/api/users/123\"}).","Attach the model to a Collection that has a url, so the model inherits the collection's URL.","Verify the property is spelled 'url'/'urlRoot' and defined before the first fetch/save/destroy call."],"exampleFix":"// before\nvar User = Backbone.Model.extend({});\nnew User({id: 42}).fetch(); // throws: A \"url\" property or function must be specified\n\n// after\nvar User = Backbone.Model.extend({urlRoot: '/api/users'});\nnew User({id: 42}).fetch(); // GET /api/users/42","handlingStrategy":"validation","validationCode":"function hasUrlTarget(modelOrCollection) {\n  if (modelOrCollection instanceof Backbone.Model) {\n    return typeof modelOrCollection.urlRoot !== 'undefined' ||\n           typeof modelOrCollection.url !== 'undefined' ||\n           (modelOrCollection.collection && modelOrCollection.collection.url);\n  }\n  return typeof modelOrCollection.url !== 'undefined';\n}\nif (!hasUrlTarget(user)) throw new Error('Model must define url or urlRoot before fetch/save');","typeGuard":"function isSyncable(m) {\n  return !!(m.urlRoot || typeof m.url === 'function' || typeof m.url === 'string' ||\n            (m.collection && m.collection.url));\n}","tryCatchPattern":"try {\n  model.fetch();\n} catch (e) {\n  if (/url.*property or function must be specified/.test(e.message)) {\n    model.fetch({url: '/api/' + model.constructor.name.toLowerCase() + 's/' + model.id});\n  } else { throw e; }\n}","preventionTips":["Always define urlRoot (models) or url (collections) at class definition time.","Validate model/collection URL config in app bootstrap or a base class constructor.","For nested resources, implement url() functions rather than relying on defaults.","Double-check property spelling ('url', 'urlRoot') after refactors.","Wrap low-level Backbone.sync calls with options.url whenever the target is computed at runtime."],"tags":["backbone","model","url","sync"],"backgroundTag":"missing-url-configuration","analyzedSha":"f229c75b194a63a8e07a3313ce43e8d2dc105327","analyzedAt":"2026-08-28T23:02:54.813Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}