I am working on Facebook login in Node js with Mongoose. i am getting issues while updating data.
This is my Scheme file
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Schema
var RegSchema = mongoose.Schema({
UserName: String,
UserEmail: String,
userprofileImage : String,
userId: String,
reg_time : {
type : Date, default: Date.now
}
}, { collection: 'user' });
// Model
module.exports = mongoose.model('UserReg', RegSchema);
and this is my main file where update and insert(if not found) is placed
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var UserReg = require('./myschema.js');
module.exports = function(userdetail, callback){
var bb;
var UserAdd = new UserReg({
"UserName": userdetail.username,
"UserEmail": userdetail.useremail,
"userId": userdetail.userId,
"userprofileImage": userdetail.userprofileImage
});
var userdetailchange = {
"UserName": userdetail.username,
"UserEmail": userdetail.useremail,
"userId": userdetail.userId,
"userprofileImage": userdetail.userprofileImage
};
// Update if found otherwise Insert
UserAdd.update({"userId":userdetail.userId },{ $set:userdetailchange, $setOnInsert:userdetailchange ,upsert:true, new: true, setDefaultsOnInsert: true}, function (err, data) {
if (err) {
callback(err);
}
callback(data);
});
};
Now it keeps on inserting user data in collection with only 1 field which is userId and also a default _id field. Please help me in updating whole data if match found or Insert whole data in Collection. Any help will be highly appreciated.
via noobcode
No comments:
Post a Comment