Asim-Tahir
9/15/2019 - 3:26 PM

➰ Mongoose Connection

const mongoose = require('mongoose');

let connection = {
	Products: mongoose.model('Products', new mongoose.Schema({
		title: String,
		categories: Array,
		isItNew: Boolean,
		discountAmout: String,
		coverImg: String,
		imgsPath: Array,
		oldPrice: String,
		price: String,
		isLiked: Boolean,
		starCount: Number,
		width: String,
		haveSizes: Array,
		haveColor: Array
	})),
	
	let mongooseDB = {
		name: 'test',
	}

	getDocumentData(DocumentName){
    mongoose.Promise = global.Promise;
		const localUri = "mongodb://localhost:27017/"+mongooseDB.name;
		// mongoose.createConnection(localUri, {useNewUrlParser: true, useUnifiedTopology: true});
    mongoose.connect(localUri, {useNewUrlParser: true, useUnifiedTopology: true});
    mongoose.connection.on('error', errConn => {
      console.error('Connection Error:\n'+errConn);
		});
		
		switch(DocumentName) {
			case 'Products':
				this.Products.find((err, data) => {
					if(!err) return data[0];
					else console.error(err);
				})
				
				// this.Products .findOne({}, (err, data) => {
				// 	if(err) console.error(err);
				// 	return data;
				// });
						
			default:

				break;
		}
	}
}
module.exports = connection;