URL shortener appears to work, but fails user stories 2 and 3. Console: "CastError: Cast to Number failed..."

Hello, and thank you for reading. First, a link to my URL Shortener repl.it: https://repl.it/@HeapOfPackrats/boilerplate-project-urlshortener#server.js

I am able to pass in a variety of URLs containing http:// or https:// and get the expected {“original_url”: String, “short_url”: Number} output, but the tests for user stories 2 and 3 are failing. I’ve tried removing dns.lookup, but that didn’t help. I suspect I may be having trouble due to an auto-incrementing library for Mongoose (AutoIncrement): I’m seeing a lot of errors like what I’ve pasted below. I would appreciate any assistance finding any errors/solutions to my inability to pass the tests.

CastError: Cast to Number failed for value "undefined" at path "shorturl" for model "ShortUrl"
    at model.Query.exec (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/query.js:4358:21)
From previous event:
    at promiseOrCallback (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at model.Query.exec (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/query.js:4369:10)
    at /home/runner/boilerplate-project-urlshortener/server.js:88:66
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/layer.js:95:5)
    at /home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:281:22
    at param (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:354:14)
    at param (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:410:3)
    at next (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:275:10)
    at urlencodedParser (/home/runner/boilerplate-project-urlshortener/node_modules/body-parser/lib/types/urlencoded.js:91:7)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:317:13)
    at /home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/index.js:275:10)
    at cors (/home/runner/boilerplate-project-urlshortener/node_modules/cors/lib/index.js:188:7)
    at /home/runner/boilerplate-project-urlshortener/node_modules/cors/lib/index.js:224:17
    at originCallback (/home/runner/boilerplate-project-urlshortener/node_modules/cors/lib/index.js:214:15)
    at /home/runner/boilerplate-project-urlshortener/node_modules/cors/lib/index.js:219:13
    at optionsCallback (/home/runner/boilerplate-project-urlshortener/node_modules/cors/lib/index.js:199:9)
    at corsMiddleware (/home/runner/boilerplate-project-urlshortener/node_modules/cors/lib/index.js:204:7)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener/node_modules/express/lib/router/layer.js:95:5) {
  messageFormat: undefined,
  stringValue: '"undefined"',
  kind: 'Number',
  value: 'undefined',
  path: 'shorturl',
  reason: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
  
    assert.ok(!isNaN(val))
  
      at castNumber (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/cast/number.js:28:10)
      at SchemaNumber.cast (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/schema/number.js:399:12)
      at SchemaNumber.SchemaType.applySetters (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/schematype.js:1110:12)
      at SchemaNumber.SchemaType._castForQuery (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/schematype.js:1545:15)
      at SchemaNumber.castForQuery (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/schema/number.js:453:14)
      at SchemaNumber.SchemaType.castForQueryWrapper (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/schematype.js:1512:20)
      at cast (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/cast.js:331:32)
      at model.Query.Query.cast (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/query.js:4759:12)
      at model.Query.Query._castConditions (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/query.js:1841:10)
      at model.Query.<anonymous> (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/query.js:2098:8)
      at model.Query._wrappedThunk [as _findOne] (/home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)
      at /home/runner/boilerplate-project-urlshortener/node_modules/kareem/index.js:370:33
      at processTicksAndRejections (internal/process/task_queues.js:79:11) {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: false,
    expected: true,
    operator: '=='
  }
}

I have gotten the following URLs to work successfully with my URL shortener both with and without dns.lookup:

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Request Header Parser Microservice

Link to the challenge:

I made some POST requests to that backend and always received: invalid url.

Not sure why it works with the form, but not when making the calls directly using CURL:

curl --request POST \
  --url https://boilerplate-project-urlshortener--heapofpackrats.repl.co/api/shorturl/new \
  --header 'Content-Type: application/json' \
  --data '{
	"url":"https://www.blah.com/"
}'

I don’t know how to start a quick Mongodb instance to debug this on repl.it, but that curl command might help you find a clue.

Hello, and thanks for responding. I am getting the expected result back when I POST with a Form URL Encoded req body:

That might be the issue… I’d try to make it work with json and see if it helps.

I have added app.use(express.json());, and now I can handle JSON-formatted POSTs. However, GET on /api/shorturl/:shortUrl is still bugging out with the error pasted in the original post. I suppose I should handle :shortUrl that aren’t Number since I output short URLs as integers, but I still don’t understand why FCC’s tests won’t pass.

Edit: it might be my regex that’s failing.

Edit 2: it was my regex failing to match some of the test URLs’ subdomains (evil, evil hyphens)