These are chat archives for Automattic/mongoose

26th
Jan 2016
antonioaltamura
@antonioaltamura
Jan 26 2016 01:11
guys It's a pretty newbie question. I have a schema who contains an array of refs. But I gets an error if I try to save a single value.
"Cast to Array failed for value". How you handle the unsure amount the user could fill (0, 1 or 1+) in case of array of refs?
J
@jhyland87
Jan 26 2016 03:33
@alfredopacino " How you handle the unsure amount the user could fill (0, 1 or 1+) in case of array of refs?" Not sure what you mean? you mean check if its an array or not? Just.. check if its an array, lol. use typeof or underscores/lodashes _.isArray
J
@jhyland87
Jan 26 2016 15:33
Hey guys. Im running into a rather curious error. it saying I have a duplicate key error. I usually get this when I try to insert a duplicate value in a field thats set to unique: true in the schema, but this one doesnt have any unique restrictions... So shouldnt I be able to run the same insert script twice?
This message was deleted
J
@jhyland87
Jan 26 2016 15:42
This message was deleted
This message was deleted
This message was deleted
This message was deleted
Nvm the above, got it, just had to clear the indexes. (Tried to delete some messages, to clear some space, hate flooding chats)
Chris Dillinger
@ChrisDillinger
Jan 26 2016 16:49
If i'm trying to use mongoose , but on npm it says the build is 'failing'... what does that mean I should do?
should i try to install an older version? i.e.,, is using "npm install mongoose" not what I wanna do right now? --> Just hacking something together for fun
Konstantin Baierer
@kba
Jan 26 2016 16:50
@ChrisDillinger No, that "build failing" message is just the continuous integration that reported errors. You needn't worry about that too much.
Chris Dillinger
@ChrisDillinger
Jan 26 2016 16:51
thx!
Konstantin Baierer
@kba
Jan 26 2016 16:51
Don't use an older version, there are improvements and security fixes all the time.
J
@jhyland87
Jan 26 2016 18:36
This message was deleted
antonioaltamura
@antonioaltamura
Jan 26 2016 23:27

Hi guys this is my json data I send in POST to a resource

{ content: 'mickey',
  reg: 
   [ { number: '777', year: '666' },
     { number: '555', year: '444' } ],
  postedBy: '56a60a972b70225014753d1a' }

this is the schema

var com_post_schema = new Schema({
    content: { type: String, required: true },
    reg:[{ number: Number, year: Number}],
    postedBy: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'com_user'
    }]
});

and this is the route

app.post('/api/community/posts', function(req,res){
    var com_post1= new com_post({
        content:req.body.content,
        postedBy:req.body.postedBy,
        reg:req.body.reg
    });
    com_post1.save(function(err){
        if(!err)
            res.send("New post added! "+req.body.content);
        else {      res.send("Error adding post");      }
    });
});

The "reg" field in db is always empty. I'm on in days with this problem! :(