Express/EJS/Mongo Data

Hi all.

I’m learning Node/Express/Mongo and I have a question. I currently have a data scheme that looks like this:

{
  name: String,
  image: String,
  description: [String],
  rank: Number
}

On the form provided, they can enter up to 10 description items (there are 10 form fields) which are then put into the description array and stored in the database. I then want to automatically assign them a rank based on the number of descriptions they enter. For example, if they enter a description in two of the fields (so 2 descriptions provided), they would receive a rank of 2 and that rank would be stored in the database.

How would I accomplish this? I understand the if/then logic that could make it happen but how would I actually compute this and then store it in the database?

You could do something like…

var description = ["String", "String"];

var yourUser = {
  "name": "String",
  "image": "String",
  "description": description,
  "rank": description.length
}

db.collection('users').insertOne(yourUser);

Hello and thank you for the reply.

I apologize if this question makes no sense but I’m not exactly sure what you’re saying.

Currently, my post route looks like this:

app.post('/play', function(req, res) {
  Card.create(req.body.card, function(err, newCard) {
    res.redirect('/play');
  }
}

Are you saying I should change the post request itself?

I’m currently learning this also. From what I understand, your POST needs to pass the variable(s) to the “play file”. I assume you have a “Card” function/class that uses the user’s entries(variables) of descriptions? I just added comments to the code above(code from codemzy, thanks)

//This creates a variable for the amount of descriptions the user entered into an array(your example was 2), so 2 strings in example
var description = ["String", "String"];

//This creates the user's entry
var yourUser = {
"name": "String",
"image": "String",
"description": description,   //this is the array, created above with the 'var description', that contains amount of descriptions the user entered
"rank": description.length   //this will assign the rank based on how many descriptions are stored in THIS array
}

db.collection('users').insertOne(yourUser);  //creates/sends the user's entry to the database collection named 'users'

How are you creating the Card object?

app.post('/play', function(req, res) {
Card.create(req.body.card, function(err, newCard) {
res.redirect('/play');
}
}

There are lots of different patterns and ways you could do this so have a play around with what works for you. I just based the above example on the object you already had… I may have this wrong (not seeing the rest of your code) but are you saying that the req.body.card already has…

{
  "name": "String",
  "image": "String",
  "description": ["String", "String"]
}

And you just want to add the rank before passing the data to Card.create?

You could do something like…

app.post('/play', function(req, res) {
  var cardObj = req.body.card; // get the data from the req.body
  cardObj.rank = cardObj.description.length; // add the rank property
  // pass the completed object to Card.create
  Card.create(cardObj, function(err, newCard) {
    if (err) {
      // don't forget to handle any errors
    }
    res.redirect('/play');
  }
}
1 Like

Sorry that it took so long for me to respond. This worked! Thank you very much for your help!

1 Like