These are chat archives for Automattic/mongoose

19th
Mar 2018
Sedric Heidarizarei
@saeedhei
Mar 19 2018 00:00

How Can I validate min number and get Too few Message in save()?

Schema:

const boardSchema = new Schema({
  mr1: {type: Number, required: true, min: [2000, 'Too few'], max: 100000},
});

Code:

const myBoard = new Board({
  mr1: '0',
});
myBoard.save((err) => {
  if (err) {
    return err;
  }

  return console.log('saved');
});
Kev
@lineus
Mar 19 2018 00:27
I’ll be back at my desk in 30 mins or so @saeedhei :)
Kev
@lineus
Mar 19 2018 01:38
@saeedhei theres a validate option that you can use to customize validation. There are some good examples here
if you just want the custom message, that looks right at first glance.
Sedric Heidarizarei
@saeedhei
Mar 19 2018 05:26
@lineus I didn't understand how Should I fit with my example, I Searched on stackoverflow and I find an assert.equal() method, but I god assert not defined, I want Just get a min and max error in my example.
Ghost
@ghost~5928d90bd73408ce4f629b9e
Mar 19 2018 08:39

Anyone know how can i increment a value of an element inside array object?

How do i increment a value of an element in array object?

    "options": [
        {
            "key": "banana",
            "votes": 0
        },
        {
            "key": "apple",
            "votes": 0
        },
        {
            "key": "mango",
            "votes": 0
        },
        {
            "key": "grapes",
            "votes": 0
        }
    ]
            db().collection('polls').update( {_id: id, "options.key": selectedItem },
            {$inc: {"options.$.votes" : 1} })
            res.send('ok')
Kev
@lineus
Mar 19 2018 09:32
@saeedhei you have to const assert = require('assert') if you need to use assert, but min and max are built-in validators, if you set them in the schema they should work by default. Here is a simple example.
Sedric Heidarizarei
@saeedhei
Mar 19 2018 12:23
@lineus Thanks, Solved with This line return console.error(err.message);
Bruno Barros
@bybrunobarros
Mar 19 2018 14:07
Hey everyone! In the following code how can I populate the virtual attribute org.featuredTestimony.person ?
const TestimonySchema = new Schema({
  body: String,
  state: String,
  person: {type: ObjectId, ref: 'Person'}
})

const OrgSchema = new Schema({
  name: String,
  testimonies : [TestimonySchema]
})

OrgSchema.virtual('featuredTestimony').get(function () {
  return _.find(this.testimonies, testimony => testimony.state === 'featured')

})

mongoose.model('Testimony', TestimonySchema)
mongoose.model('Organization', OrgSchema)

const org = await Organization
  .findById(id, 'name featuredTestimony')
  .populate('featuredTestimony.person') // ??
Kev
@lineus
Mar 19 2018 16:00
I think that select in .findById() gets passed to the db, but the virtual only exists locally @bybrunobarros
I'll dig into it a bit unless you already sorted it out
Kev
@lineus
Mar 19 2018 17:02
here is a gist @bybrunobarros
Sedric Heidarizarei
@saeedhei
Mar 19 2018 23:31
Why I'm Getting This Error: Invalid atomic update value for $set. Expected an object, received string
I have a global let iMsg = '';
and I get iMsg from an admin and this is console.log of iMsg :
{
  msg: '2000',
}
await Board.update({_id: '5aafad719c19482ad4cf6a00'}, {$set: iMsg}, {upsert: true}, (err) => {
  if (err) {
    console.log(iMsg);
    return console.error(err.message);
  }

  return console.log('Updated');
});
await bot.sendMessage(msg.chat.id, `updated`, opts);
});
Kev
@lineus
Mar 19 2018 23:54

if your schema was

schema({
   message: String
})

and iMsg is a string, you would use $set: { message: iMsg }

I think :) I need to test it to be sure