简体   繁体   English

NODEJS,我如何正确地将我的容器 class 的这个方法导出到我管理 express 的另一个 JS 文件?

[英]NODEJS, how do I correctly export this method of my Container class to another JS file where I manage express?

[I'm trying to pass my getAll() method of my Container class found in app.js to my appServer.js file to pass it to the res.send() of my app = express() variable and pass that res.send() to the route "/products" [我试图将我在 app.js 中找到的容器 class 的 getAll() 方法传递到我的 appServer.js 文件,以将其传递到我的 app = express() 变量的 res.send() 并传递该 res。 send() 到路由“/products”

when I try to run the server it throws me an error telling me that Container.getAll() is not a function当我尝试运行服务器时,它抛出一个错误告诉我 Container.getAll() 不是 function

I have tried to export using exports.getAll() but it still throws me an error]我尝试使用 exports.getAll() 导出,但它仍然会抛出一个错误]

[App.js] [App.js]

const fs = require("fs");

class Contenedor {
  constructor(txtNameFile) {
    this.txtNameFile = txtNameFile;
    this.products = [];
  }

  async fileInJSON() {
    let fileTxt = await fs.promises.readFile(this.txtNameFile, "utf-8");
    let type = JSON.parse(fileTxt);
    return type;
  }

  async fileSaving(item) {
    let type = JSON.stringify(item);
    await fs.promises.writeFile(this.txtNameFile, type);
  }

  async save(obj) {
    try {
      let fileTxt = await fs.promises.readFile(this.txtNameFile, "utf-8");
      if (fileTxt === "") {
        obj.id = 1;
        this.products.push(obj);
      } else {
        const type = JSON.parse(fileTxt);
        obj.id = type[type.length - 1].id + 1;
        type.push(obj);
        this.products = type;
        this.fileSaving(type);
      }
      console.log(
        "El producto se ha guardado en el archivo satisfactoriamente"
      );
      return obj.id;
    } catch (error) {
      console.error("No se ha podido guardar");
    }
  }

  async getById(id) {
    let type = await this.fileInJSON();
    let product = type.find((product) => product.id == id);
    return console.log(product);
  }

  async getAll() {
    let type = await this.fileInJSON();
    return console.log(type);
  }

  async deleteAll() {
    let item = [];
    this.products = item;
    this.fileSaving(item);
  }

  async deleteById(number) {
    let type = await this.fileInJSON();
    let item = type.find((item) => item.id === number);
    let index = type.indexOf(item);
    type.splice(index, 1);
    this.fileSaving(type);
  }
}

module.exports = Contenedor;

[appServer.js] [应用服务器.js]

const express = require("express");
const app = express();
const PORT = 3000;
const Contenedor = require("./app.js");
 

app.get("/", (req, res) => {
  res.send("Ingresa a la ruta /productos para ver los productos :D");
});

app.get("/productos", (req, res) => {
  res.send(Contenedor.getAll());
});

const servidor = app
  .listen(PORT, () => {
    console.log(`Servidor corriendo en el puerto ${PORT}`);
  })
  .on("error", (error) => console.error("FALLASTE" + error));

Maybe you should instantiate a Container object by using new operator.也许您应该使用 new 运算符实例化一个 Container object。

const express = require("express");
const app = express();
const PORT = 3000;
const Contenedor = require("./app.js");
const myContainer = new Contenedor("example.txt");
 

app.get("/", (req, res) => {
  res.send("Ingresa a la ruta /productos para ver los productos :D");
});

app.get("/productos", (req, res) => {
  res.send(myContainer.getAll());
});

const servidor = app
  .listen(PORT, () => {
    console.log(`Servidor corriendo en el puerto ${PORT}`);
  })
  .on("error", (error) => console.error("FALLASTE" + error));

you should access to that class first.您应该首先访问该 class。

app.get("/productos", (req, res) => {
    const test_class = new Contenedor()
    res.send(test_class.getAll());
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将此变量从类方法导出到另一个文件? - How do I export this variable from class method to another file? 如何正确地将一个普通的 JS 文件导出和导入到另一个 JS 文件中? - How do I correctly export and import a plain JS file into another JS file? 如何在Node.js中正确导出函数? - How do I export a function correctly in nodejs? NodeJS(express)我将如何完成此.js文件和.ejs文件以在浏览器中查询objectiD? - NodeJS(express) How would i finish this .js file and .ejs file to query the objectiD within my browser? JS:如何在另一个文件中使用初始化的 class? - JS: How do I use an initialized class in another file? 如何将value变量从javascript文件夹中的普通js文件转换为nodejs中的route / index.js文件并进行表达? - How do i take the value variable from a normal js file in the javascript folder to the routes/index.js file in nodejs and express? 如果我使用 jQuery 的 ajax 方法将 CSV 文件发送到服务器,文件将位于我的 Node.js + Express 服务器上的什么位置? - Where would a file be located on my Node.js + Express server if I send a CSV file to the server using jQuery's ajax method? 我在Express JS中丢失URL的地方 - Where i lost my URL in express js 如何将对象数组及其属性导出到另一个 JS 文件? - How do I export an array of objects and it's properties to another JS file? 如何在 nodejs 中设置导出默认值? - How do i make an export dafault in nodejs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM