-
Notifications
You must be signed in to change notification settings - Fork 0
/
readArray.js
56 lines (48 loc) · 1.26 KB
/
readArray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const mongoose = require("mongoose");
const express = require("express");
const app = express();
const router = express.Router();
const port = 3000
app.get('/', (req, res) =>
{
res.send('Hello World!')
})
app.listen(port, () =>
{
console.log(`Example app listening on port ${port}`)
})
const ProductSchema = new mongoose.Schema(
{ id: Number, name: String, price: String, qty: Number, brand: String },
{ versionKey: false }
);
mongoose.connect("mongodb://0.0.0.0:27017/ecommerce", {
useNewUrlParser: true,
useUnifiedTopology: true
});
const ProductModel = mongoose.model("Model", ProductSchema, "products");
//find : Array : Collection Object
//findOne : Object
let Products = {}
ProductModel.find({}, (error, data) =>
{
if (typeof (data) == 'object')
{
if (Array.isArray(data))
{
console.log("Data is Array of Object ");
// console.log(data)
Products = data;
} else
{
console.log("Data is Object");
console.log(data)
}
}
mongoose.disconnect();
});
app.use('/products', router)
router.get('/find', (req, res, next) =>
{
// res.writeHead(200, { "Content-Type": "application/json" });
res.send(JSON.stringify(Products, null, 4));
})