These are chat archives for Automattic/mongoose

23rd
Jun 2016
Alex Shpak
@ishikawayoshi
Jun 23 2016 07:26
@ycpatel813 i use angular in my frontend, with angular it's easy to do with default filter like {{ yourdate | date:'YYYY-MM-DD'}}
valli
@bunnypolisetty_twitter
Jun 23 2016 09:50
hai all.....i'm tring to add data to db using mongoose but im unable to add ........
Alexander
@damongant
Jun 23 2016 09:50
you wot
show us some code, just saying it doesn't work isn't very helpful
valli
@bunnypolisetty_twitter
Jun 23 2016 09:51
$http({
method: 'POST',
data: {
id: " "
name: " ",
color: " ",
size: " ",
price: " ",
category: " "
    },
    url: '/api/insert'

}).then(function successCallback(response) {
    console.log("resp -->", response);
    $scope.resp = response.data;
    // this callback will be called asynchronously
    // when the response is available
}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});
Alexander
@damongant
Jun 23 2016 09:52
Hey, please use a paste
and that doesn't look like mongoose code
Mohamed Nasrullah
@nasr18
Jun 23 2016 09:52
its a frontend code
valli
@bunnypolisetty_twitter
Jun 23 2016 09:52

app.post('api/insert', function (req, res) {
console.log("insert watches---->");
var col = new newcol({
"name": "TIMEWELL",
"color": "WHITE",
"size": "M",
"price": "2200",
"id": w11,
"category": "men"

});
col.save(function (err) {

    if (err) {
        console.log("err--->", err);
        res.send(err);

    } else {
        console.log(col);
    }
});

});//this is my mongoose code

Mohamed Nasrullah
@nasr18
Jun 23 2016 09:52
pose backend code
valli
@bunnypolisetty_twitter
Jun 23 2016 09:53
im unable to post this data to db......
Mohamed Nasrullah
@nasr18
Jun 23 2016 09:53
save(function(err, data) {.....}
pass data parameter in save callback
Alexander
@damongant
Jun 23 2016 10:00
that's hardly the issue
valli
@bunnypolisetty_twitter
Jun 23 2016 10:02
issue isn't resolved:-(
idont know where the problem is.....
Alexander
@damongant
Jun 23 2016 10:03
if you can push the entire thing somewhere I can test it, but like this is, well, it could be anything
do you get an error on the console?
valli
@bunnypolisetty_twitter
Jun 23 2016 10:04
yep....cannot post

//userCtrl.js$http({
method: 'POST',
data: {
id: " "
name: " ",
color: " ",
size: " ",
price: " ",
category: " "
},
url: '/api/insert'

}).then(function successCallback(response) {
console.log("resp -->", response);
$scope.resp = response.data;
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});

Alexander
@damongant
Jun 23 2016 10:05
the frontend code is not relevant
valli
@bunnypolisetty_twitter
Jun 23 2016 10:05

//server.js app.post('api/insert', function (req, res) {
console.log("insert watches---->");
var col = new newcol({
"name": "TIMEWELL",
"color": "WHITE",
"size": "M",
"price": "2200",
"id": w11,
"category": "men"

});
col.save(function (err, data) {

    if (err) {
        console.log("err--->", err);
        res.send(err);

    } else {
        console.log(data);
    }
});

});

Alexander
@damongant
Jun 23 2016 10:06
yeah, you posted that, but it doesn't help, what is the error message?
Mohamed Nasrullah
@nasr18
Jun 23 2016 10:07
what error u r getting???
valli
@bunnypolisetty_twitter
Jun 23 2016 10:07
error msg is 404 not found
Mohamed Nasrullah
@nasr18
Jun 23 2016 10:08
in else conditoin u dint send any data to view.
send data to view from server.js
Alex Shpak
@ishikawayoshi
Jun 23 2016 10:11
maybe error is here
app.post('api/insert'
app.post('/api/insert'
valli
@bunnypolisetty_twitter
Jun 23 2016 10:18

var newcollection = mongoose.model('newcol', wSchema, 'newcol'); app.post('/api/insert', function (req, res) {
console.log("insert watches---->");
var col = new newcol({
"name": "TIMEWELL",
"color": "WHITE",
"size": "M",
"price": "2200",
"id": w11,
"category": "men"

});
col.save(function (err, data) {

    if (err) {
        console.log("err--->", err);
        res.send(err);

    } else {
        console.log(data);
        res.send(data);
    }
});

});

my error is newcol is not defined
Alex Shpak
@ishikawayoshi
Jun 23 2016 10:19
try to use newcollection
var col = new newcol
and i can mistaken but do we need here third param?
var newcollection = mongoose.model('newcol', wSchema, 'newcol');
valli
@bunnypolisetty_twitter
Jun 23 2016 10:24
i think the third argument to the method actually specifies the explicit name to use for the collection
is it necessary to specify _id before saving an document to db...if 'yes' y??
valli
@bunnypolisetty_twitter
Jun 23 2016 10:29
my error is to specify _id
Alex Shpak
@ishikawayoshi
Jun 23 2016 10:30
if you not specify id it added by default
with random hash
if i understand right you told about http://mongoosejs.com/docs/api.html#index_Mongoose-model
what is you error now?
valli
@bunnypolisetty_twitter
Jun 23 2016 10:31
specify _id before saving
Alex Shpak
@ishikawayoshi
Jun 23 2016 10:34
how do you define _id in schema?
valli
@bunnypolisetty_twitter
Jun 23 2016 10:36

var wSchema = new Schema({
_id: String,

name: String,
color: String,
price: String,
size: String,
id: String,
category: String

}, {
strict: false
});

Alex Shpak
@ishikawayoshi
Jun 23 2016 10:37
btw use ``` before and after your code when you paste it in gitter
valli
@bunnypolisetty_twitter
Jun 23 2016 10:40

```var wSchema = new Schema({
_id: String,

name: String,
color: String,
price: String,
size: String,
id: String,
category: String

}, {
strict: false
});```

Alex Shpak
@ishikawayoshi
Jun 23 2016 10:41
use shift + enter after ```, this markdown shoud be on a separate line
valli
@bunnypolisetty_twitter
Jun 23 2016 10:42
var wSchema = new Schema({
    _id: String,

    name: String,
    color: String,
    price: String,
    size: String,
    id: String,
    category: String

}, {
    strict: false
});
tq alex
Alex Shpak
@ishikawayoshi
Jun 23 2016 10:44
"price": "2200",
"id": w11,
"category": "men"
what is w11?
do you need wrap it in quotes or you specify it before?
valli
@bunnypolisetty_twitter
Jun 23 2016 10:46
v need to give it as "w11" i modified it later..now my issue is _id need to specify..
Alex Shpak
@ishikawayoshi
Jun 23 2016 10:47
check "_id" in schema and model "id"
it looks you forget underscore
oh, u have two _id,id in schema why do you need both?
i think it's enough one "id"
Alex Shpak
@ishikawayoshi
Jun 23 2016 10:54
_id will set by mongo by default, you don't need to set it in schema
valli
@bunnypolisetty_twitter
Jun 23 2016 10:54
id is user defined id for my use....and _id is the id which generates objectId
bt objectId is not generated by default when my document is inserted
Alex Shpak
@ishikawayoshi
Jun 23 2016 10:57
here is simple code of model from https://scotch.io
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// create a schema
var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
    age: Number,
    website: String
  },
  created_at: Date,
  updated_at: Date
});

// the schema is useless so far
// we need to create a model using it
var User = mongoose.model('User', userSchema);

// make this available to our users in our Node applications
module.exports = User;
as u can see you don't need to set "_id" here and it works fine
valli
@bunnypolisetty_twitter
Jun 23 2016 11:01
hey dats cool...
got it
tq:-)
Alex Shpak
@ishikawayoshi
Jun 23 2016 11:02
and btw you don't need to add strict:false it false by default
valli
@bunnypolisetty_twitter
Jun 23 2016 11:08
the document is inserting into db when i run the code evry time..i dont want that to be happen....
Alex Shpak
@ishikawayoshi
Jun 23 2016 11:12
when you refresh page your ajax code sends every time request to server where mongoose save data to db, change logic of execution ajax, add listener to btn or something else, maybe this is a reason
Paul "Joey" Clark
@joeytwiddle
Jun 23 2016 13:59
In Mongoose 3 and 4, strict defaults to true. http://mongoosejs.com/docs/guide.html#strict
Alex Shpak
@ishikawayoshi
Jun 23 2016 14:02
@joeytwiddle thanks Paul for updating my answer
Paul "Joey" Clark
@joeytwiddle
Jun 23 2016 14:03
You are welcome sir :)
I actually set strict:'throw' for a while on our app, so it would catch my mistakes when I tried to write a field not in the schema. (By default it just ignores/discards unknown fields.)
Alex Shpak
@ishikawayoshi
Jun 23 2016 14:06
yeah, cool thing