简体   繁体   English

如何从前端 React 以编程方式添加到数据库中的数组

[英]How to programmatically add to an array in database from frontend React

I basically have this route我基本上有这条路线

app.post("/order/:orderId/:productId", async (req, res) => {
  const result = await Order.findByIdAndUpdate(
    req.params.orderId,
    {
      $push: {
        products: req.params.productId
      }
    },
    { new: true }
  );
  res.send(result);
});

I have two collections, namely Product and Orders.我有两个集合,即产品和订单。 The goal is to get, for instance, a particular Order with id(5ddfc649e1e9e31220ce6a16) , and post a product with id(5de02c4a0ed3160368b9a550) inside an Array field inside this Orders collection.例如,目标是获取 id(5ddfc649e1e9e31220ce6a16) 的特定订单,并在此 Orders 集合内的 Array 字段中发布带有 id(5de02c4a0ed3160368b9a550) 的产品。 In Postman, I can do that manually by just adding the ObjectIds in the URL like so:在 Postman 中,我可以通过在 URL 中添加 ObjectIds 来手动完成,如下所示:

Http://localhost:3000/orders/5ddfc649e1e9e31220ce6a16/5de02c4a0ed3160368b9a550.

and I get this Response :我得到这个回应:

{
    "products": [
        "5ddfb388b14c5b41e0607a5e",
        "5de02c4a0ed3160368b9a550" // newly added Product


    ],
    "_id": "5ddfc649e1e9e31220ce6a16",
    "issuedBy": "issuedBy",
    "collectedBy": "collectedBy",
    "quantity": 123,
    "createdAt": "2019-11-28T11:48:40.500Z",
    "updatedAt": "2019-11-28T11:59:51.659Z",
    "__v": 0
}

My challenge is, how do I do this programmatically from the UI(Reactjs) side?我的挑战是,如何从 UI(Reactjs) 端以编程方式执行此操作?

// Product Schema

const mongoose = require("mongoose");

const ProductSchema = new mongoose.Schema(
  {
    name: String,
    description: String,
    price: Number,
    quantity: Number,
    supplier: String
  },
  { timestamps: true }
);

module.exports = mongoose.model("Product", ProductSchema);

// Orders Schema

const mongoose = require("mongoose");

const OrderSchema = new mongoose.Schema(
  {
    issuedBy: String,
    collectedBy: String,
    quantity: Number,
    products: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Product",
        required: true
      }
    ]
  },
  { timestamps: true }
);

module.exports = mongoose.model("Order", OrderSchema);

I would really appreciate any suggestion or a sample code snippet我真的很感激任何建议或示例代码片段

I think what you are looking for is, you need to hit this API from your front end ie React app.我认为你正在寻找的是,你需要从你的前端即 React 应用程序点击这个 API。

http://localhost:3000/orders/<order-id-here>/<product-id-here>

And then supply the dynamic value for your order id and product id.然后为您的订单 ID 和产品 ID 提供动态值。

You can hit api using fetch.您可以使用 fetch 访问 api。 More info更多信息

You can simply take some variables in your front end app.您可以简单地在前端应用程序中使用一些变量。 As I don't know how your order id and product id are being generated so you need to check on that and then something like,因为我不知道您的订单 ID 和产品 ID 是如何生成的,所以您需要检查一下,然后进行类似的检查,

let orderId = someValue
let productId = someOtherValue
let url = "http://localhost:3000/orders/"+orderId+"/"+productId

But I think the concern would be GET and POST method.但我认为关注点是 GET 和 POST 方法。 Right now you have app.post but the params are in URL so instead you should go for GET method ie app.get现在你有app.post但参数在 URL 中,所以你应该使用 GET 方法,即app.get

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

相关问题 如何使用 React 从后端数据库中获取以显示在前端? - How to fetch from backend database to display on frontend using react? 从数组中提取对象时减少返回 NaN 的函数 - MongoDB 数据库,React 前端 - Reduce function returning NaN when pulling object from array - MongoDB database, React frontend 如何将反应前端添加到 Spring Boot Rest? - How to add react frontend to spring boot rest? 尝试对数据库数组中的值求和,在 React 前端返回“NaN”错误 - Attempting to sum values in database array returning 'NaN' error in React frontend 如何在React中将数据从后端推送到前端 - How to push data from backend to frontend in react 如何从后端(nodejs)重定向到前端(react)? - How to redirect from backend (nodejs) to frontend (react)? 如何在反应前端显示来自strapi的图像? - How to display image from strapi in react frontend? 如何从数据库中向 Keplergl React 添加数据 - How to add data on keplergl React from database 如何从 java servlet 发送一个数组作为 JSON 以使用 fetch() 响应前端? - How to send an array as JSON from java servlet to react frontend using fetch()? 从 Django 数据库显示图像到 React JS 前端 - Display image from Django database to React JS frontend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM