DEV Community

ivkeMilioner
ivkeMilioner

Posted on • Edited on

1 1

Problem with req. query;

I'm trying to pull data from a document containing a logged-in user from the MongoDB database. However, req. query doesn't seem to pass.

API:

export default async function handler(req, res) {
  const client = await clientPromise;
  const db = client.db("USERS");
  const { userName } = req.query;
  switch (req.method) {
  case "POST": {
    const bodyObject = JSON.parse(req.body);
    const query = {
      user: bodyObject.user,
      avatar: bodyObject.avatar,
      level: bodyObject.level,
    };
    const update = { $set: { score: bodyObject.score } };
    const options = { upsert: true };

    const newScore = await db
      .collection("USERS")
      .findOneAndUpdate(query, update, options);

    res.json(newScore);
    break;
  }
  case "GET": {
    const users = await db
      .collection("USERS")
      .find({ user: { $in: [userName] } })
      .toArray();
    res.json(users);
    break;
  }

  default: {
    // do nothing
  }
  }
}
Enter fullscreen mode Exit fullscreen mode

COMPONENT:

import styles from '../styles/Elements.module.css';
import clientPromise from '../lib/mongodb';


export default function ScoreHeader({ users }) {
  console.log(users)

  return (
    <div className={styles.containerIndex}>
     fadsfds
    </div>
  );
}

export async function getServerSideProps({}) {
  const client = await clientPromise;
 const db = client.db('USERS');

  const res = await fetch(`http://localhost:3000/api/usersAPI`, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  });
  const users = await res.json();

  return {
    props: { users },
  };
}
Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More