ejs icon indicating copy to clipboard operation
ejs copied to clipboard

Passing array of objects to MongoDB

Open csharsha opened this issue 7 years ago • 1 comments

Hi,

I have an Array of Objects with size of 81. It is as below

Array.length = 81 [1] Object {val: 1, candidates: Array[9]}

Like this I have 81 Objects. I am trying to pass this Array from EJS file hidden field to mongoose schema, but when I am trying to save this array it is getting saved as String in MongoDB, something like this "[object Object],[object Object],[object Object],[object Object]............"

So I cannot evaluate this while I want to retrieve this data. Mongoose Schema is as below

var tbl_GameData = new mongoose.Schema({
     //id: {type: Number},
     LevelID : {type: Number},
     Data : {type: Array} 
   });

and saving from ejs file is like as below

app.post('/saveGame', function(req, res) {

           var gameData = new database.GameData({
	      LevelID : 1,
	      Data : req.body.gameData
});

gameData.save(function(err) {
	if(err){
		console.log(err);
	}
	
	req.login(gameData, function(err){
		if(err){
			req.redirect('/login');
		}
		else {
			res.render('startGame',{ user: req.user ,title:"Sudoku Online Match"});
		}
    });
	
   });
   });

I dont know how to save this in DB so that I can retrieve it back as an array. Can you please help!

csharsha avatar Nov 26 '16 20:11 csharsha

This version of EJS is no longer maintained. The current version (installable by NPM) is here: https://github.com/mde/ejs

mde avatar Nov 26 '16 21:11 mde