Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Items.map is not a function for routes product ordering
Post Flair (click to view more posts with a particular flair)
Post Body

import express from 'express'
import { Order } from '../models'


const router = express.Router()

router.get("/test", async (request, response) => {
  response.json("Orders backend reached");
});


router
  .route('/')
  .all((req, res, next) => {
    next()
  })
  .get(async (req, res, next) => {
    const orders = await Order.find()
    res.send(orders)
  })
  .post(async (req, res, next) => {
    const {
      customerDetails: { firstName, lastName, email, address1, address2 },
      items,
      orderTotal,
    } = req.body
    const itemIdList = items.map((i) => i._id)
    const orderData = {
      customerName: `${firstName} ${lastName}`,
      customerEmail: email,
      customerAddress1: address1,
      customerAddress2: address2,
      items: itemIdList,
      orderTotal: orderTotal,
    }
    try {
      const order = new Order(orderData)
      order.save()
      res.json(order._id)
    } catch (error) {
      next(new Error('Error Placing Order'))
    }
  })
  .delete((req, res, next) => {
    next(new Error('not implemented'))
  })

module.exports = router

I am currently testing this backend with Postman and am trying to Post with this

{
    "customerDetails": {
        "firstName": "Sam",
        "lastName": "Iam",
        "email": "abc@abc",
        "address1": "123 Street",
        "address2": "6B"
    },
    "items": [],
    "orderTotal": 29.99
}

The schema attached to the route is

import mongoose from 'mongoose'
const { ObjectId } = mongoose.Schema.Types

const orderSchema = new mongoose.Schema(
  {
    customerName: {
      type: String,
      required: true,
    },
    customerEmail: {
      type: String,
    },
    customerAddress1: {
      type: String,
      required: true,
    },
    customerAddress2: {
      type: String,
    },
    items: [
      {
        type: ObjectId,
        ref: 'Product',
      },
    ],
    orderTotal: {
      type: Number,
      // required: true,
    },
  },
  { timestamps: true }
)

const Order = mongoose.model('Order', orderSchema)

export default Order

Whenever I try to send the JSON data to that backend, it crashes the entire app and responds with items.map is not a function but when I tried the exact same code on another project, it didn't cause any issues with the orders route in the backend.

Nvm just fixed it. It was my postman input. Needed to add a title.

Author
Account Strength
100%
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
9,114
Link Karma
553
Comment Karma
8,165
Profile updated: 2 days ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago