Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

node.js - Error: Connection terminated unexpectedly

I am trying to connect to local postgresSQL database. It used to work in the past but now i keep getting this message:

error at getting locations: Error: Connection terminated unexpectedly at Connection. (/Users/tomerrajuan/Desktop/ocd-for-me/client/node_modules/pg/lib/client.js:132:73) at Object.onceWrapper (events.js:416:28) at Connection.emit (events.js:310:20) at Socket. (/Users/tomerrajuan/Desktop/ocd-for-me/client/node_modules/pg/lib/connection.js:108:12) at Socket.emit (events.js:322:22) at endReadableNT (_stream_readable.js:1187:12) at processTicksAndRejections (internal/process/task_queues.js:84:21)

Here is my code if you have any idea how to fix it.

App.tsx

import "./style.css";
import React, { useEffect, useState } from "react";
import Footer from "./components/atoms/Footer";
import Welcome from "./components/atoms/Welcome";
import Header from "./components/molecules/Header";
import Map from "./components/molecules/Map.js";
import SuggestLocation from "./components/molecules/SuggestLocation";
import axios from "axios";

function App() {
  const [state, setState] = useState({
    locations: [],
  });
  const { locations } = state;

  useEffect(() => {
    axios
      .get("/getLocations")
      .then(function (response) {
        setState({
          locations: response.data,
        });
        console.log("locations are: ", locations);
      })
      .catch((err) => console.log("err", err));
  }, []);

  return (
    <div className="App">
      <Header />
      <Welcome />
      <Map />
      <SuggestLocation />
      <Footer />
    </div>
  );
}

export default App;

server.js

  app.get("/getLocations", async function (req, res) {
    console.log("we are at getLocations");
    try {
      const results = await db.getLocations();
      let locations = results.rows;
      res.json(locations);
    } catch (err) {
      res.json(false);
      console.log("error at getting locations: ", err);
    }
  });

error happening here

console.log("error at getting locations: ", err);

and db.js

exports.getLocations = function () {
  console.log("selecting all from locations");
  return db.query("SELECT * FROM locations");
};


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...