Home > Back-end >  Not receiving response from db, API resolved without sending a response for /api/dates, this may res
Not receiving response from db, API resolved without sending a response for /api/dates, this may res

Time:07-10

I am trying to fetch a collection of dates from my database in a very simple way, but I don't get the response (im trying to log the reponse data to the console and getting nothing). The only feedback im getting that anything is happening when I pick a date in the datepicker, is this message in the console:

API resolved without sending a response for /api/dates, this may result in stalled requests.

It also doesn’t seem that the app talk to the db at all, I tried putting invalid credentials in the DB URI and got not error in result.

/pages/api/dates.ts:

import type { NextApiRequest, NextApiResponse } from 'next'
import { connectToDatabase } from '../../lib/mongodb'

export default async function handler(
  res: NextApiResponse,
  req: NextApiRequest
) {
  if (req.method === 'GET') {
    try {
      const { db } = await connectToDatabase()
      const dates = await db.collection('dates').find({})
      return res.status(200).json({
        message: JSON.parse(JSON.stringify(dates)),
        success: true,
      })
    } catch (error: any) {
      return res.json({
        message: error.message,
        success: false,
      })
    }
  }
}

the relevant request part from /components/DateAndHours.tsx, it occures when the datepicker (input field) changes:

const dateChangedHandler = async () => {
    const response = await fetch('/api/dates', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    })

    const data = await response.json()

    console.log(data)
}

/lib/mongodb.ts

import { MongoClient } from 'mongodb'

const MONGODB_URI = process.env.MONGODB_URI
const MONGODB_DB = process.env.MONGODB_DB

// check the MongoDB URI
if (!MONGODB_URI) {
  throw new Error('Define the MONGODB_URI environmental variable')
}

// check the MongoDB DB
if (!MONGODB_DB) {
  throw new Error('Define the MONGODB_DB environmental variable')
}

let cachedClient: any = null
let cachedDb: any = null

export async function connectToDatabase() {
  // check the cached.
  if (cachedClient && cachedDb) {
    // load from cache
    return {
      client: cachedClient,
      db: cachedDb,
    }
  }

  // set the connection options
  const opts: any = {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  }

  // Connect to cluster
  let client = new MongoClient(MONGODB_URI!, opts)
  await client.connect()
  let db = client.db(MONGODB_DB)

  // set cache
  cachedClient = client
  cachedDb = db

  return {
    client: cachedClient,
    db: cachedDb,
  }
}

Thank you very much for the help! I tried looking at similar questions but found no solution.

EDIT: I proceeded to connect with the API endpoint, however, the API throwing an error that the request is empty (containing no method nor body), and I can't undrestand it. when trying to log either req.method or req.body to the console im getting undefined on both.

CodePudding user response:

Turns out the error was that I flipped to order of the handler arguments req,res Now its working perfectly fine. Such a silly mistake, but happens sometimes!

  • Related