OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pass data from server action to server component

  • Thread starter Thread starter Abdullahi Abbas
  • Start date Start date
A

Abdullahi Abbas

Guest
i'm trying to pass data in my nextJS14 app from my create server action to my Home server component, specifically the value of the temperature variable so i can display it in the the jsx of my Home server component, how do I do that please.

Code:
import Image from "next/image";
import Link from "next/link";
import ProductCard from "./components/ProductCard";
import Search from "./components/Search";

interface latLong {
  latitude: string;
  longitude: string;
}
//server action
export async function create(data: string) {
  console.log(data);
  if (data != "") {
    const apiKey = "123456";
    const geoLocationRes = await fetch(
      `http://api.openweathermap.org/geo/1.0/direct?q=${data}&appid=${apiKey}`
    );
    const geoLocation = await geoLocationRes.json();
    console.log(geoLocation);

    const getLocation: latLong = {
      latitude: geoLocation[0].lat,
      longitude: geoLocation[0].lat,
    };
    const currentWeatherRes = await fetch(
      ` https://api.openweathermap.org/data/2.5/weather?lat=${getLocation.latitude}&lon=${getLocation.longitude}&appid=${apiKey}&units={metric}`
    );
    const currentWeather = await currentWeatherRes.json();
    console.log(currentWeather);

    const temperature = currentWeather.main.temp;
    // console.log(temperature);
    // console.log("input!!");
  } else {
    console.log("no input value");
  }
}
//server component
export default async function Home() {
  return (
    <>
      <h1>Display Temperature here</h1>

      <Search />
    </>
  );
}

<p>i'm trying to pass data in my nextJS14 app from my <code>create</code> server action to my <code>Home</code> server component, specifically the value of the <code>temperature</code> variable so i can display it in the the jsx of my Home server component, how do I do that please.</p>
<pre><code>import Image from "next/image";
import Link from "next/link";
import ProductCard from "./components/ProductCard";
import Search from "./components/Search";

interface latLong {
latitude: string;
longitude: string;
}
//server action
export async function create(data: string) {
console.log(data);
if (data != "") {
const apiKey = "123456";
const geoLocationRes = await fetch(
`http://api.openweathermap.org/geo/1.0/direct?q=${data}&appid=${apiKey}`
);
const geoLocation = await geoLocationRes.json();
console.log(geoLocation);

const getLocation: latLong = {
latitude: geoLocation[0].lat,
longitude: geoLocation[0].lat,
};
const currentWeatherRes = await fetch(
` https://api.openweathermap.org/data/2.5/weather?lat=${getLocation.latitude}&lon=${getLocation.longitude}&appid=${apiKey}&units={metric}`
);
const currentWeather = await currentWeatherRes.json();
console.log(currentWeather);

const temperature = currentWeather.main.temp;
// console.log(temperature);
// console.log("input!!");
} else {
console.log("no input value");
}
}
//server component
export default async function Home() {
return (
<>
<h1>Display Temperature here</h1>

<Search />
</>
);
}
</code></pre>
 
Top