Property 'find' does not exist on type' Product []?

import * as express from "express"
import {Server} from "ws"


const app = express()

export class Product {
    constructor(
        public id: number,
        public title: string,
        public price: number,
        public rating: number,
        public desc: string,
        public categories: Array<string>
    ) { }
}

export class Comment {
    constructor(
        public id: number,
        public productId: number,
        public timestamp: string,
        public user: string,
        public rating: number,
        public content: string
    ) {

    }
}

const products: Product[] = [
    new Product(1, "", 1.99, 3.5, "Angular", ["", ""]),
    new Product(2, "", 2.99, 2.5, "Angular", [""]),
    new Product(3, "", 3.99, 4.5, "Angular", [""]),
    new Product(4, "", 4.99, 1.5, "Angular", [""]),
    new Product(5, "", 5.99, 3.5, "Angular", ["", ""]),
    new Product(6, "", 6.99, 2.5, "Angular", [""]),
]

const comments: Comment[] = [
    new Comment(1, 1, "2017-02-02 22:22:22", "", 3, "1"),
    new Comment(2, 1, "2017-02-02 22:22:22", "", 3, "2"),
    new Comment(3, 1, "2017-02-03 22:22:22", "", 3, "3"),
    new Comment(4, 1, "2017-02-04 22:22:22", "", 3, "4"),
    new Comment(5, 2, "2017-02-05 22:22:22", "", 3, "5"),
]

app.get("/",(req,res) => {
    res.send("hello express")
})

app.get("/api/products", (req,res)=> {
    let result = products
    let params = req.query;
    if(params.title){
        result = result.filter((p) => p.title.indexOf(params.title) !== -1)
    }

    if (params.price && result.length > 0) {
        result = result.filter((p) => p.price <= parseInt(params.price))
    }

    if (params.category !== "-1" && result.length > 0) {
        result = result.filter((p) => p.categories.indexOf(params.category) !== -1)
    }

    
    res.json(result)
})

app.get("api/comments/:id", (req, res) => {
    console.log(req.params.id)
    res.json(comments.filter((comment: Comment) => comment.productId == req.params.id))
})

app.get("/api/product/:id",(req,res) => {
    res.json(products.find((product)=>product.id==req.params.id))
})


const server = app.listen(8000, "localhost", ()=> {
    console.log("")
})

const wsServer = new Server({port:8085});
wsServer.on("connection", websocket => {
    websocket.send(""),
    websocket.on("message", message => {
        console.log(""+ message)
    })
} )

an error was reported during compilation

error TS2339: Property "find" does not exist on type "Product[]".   




githttps://github.com/yyccQQu/18ng2
Mar.03,2021
Change target to es6, in

tsconfig or add es6 to lib


clipboard.png

Menu