mongoose-schema

I want my pre('save') mongoose function to operate only once

谁说胖子不能爱 提交于 2021-02-05 10:30:49
问题 I do not know if the exact request in title is possible, but if not; i would really appreciate an alternate solution. I have this pre save method of mongoose ownerSchema.pre("save", function(next) { const owner = this; bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(owner.password, salt, function(err, hash) { // Store hash in your password DB. owner.password = hash; next(); }); }); }); When i save new user(owner) a hash is created successfully and all is good> The problem occurs when i

I want my pre('save') mongoose function to operate only once

半城伤御伤魂 提交于 2021-02-05 10:29:28
问题 I do not know if the exact request in title is possible, but if not; i would really appreciate an alternate solution. I have this pre save method of mongoose ownerSchema.pre("save", function(next) { const owner = this; bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(owner.password, salt, function(err, hash) { // Store hash in your password DB. owner.password = hash; next(); }); }); }); When i save new user(owner) a hash is created successfully and all is good> The problem occurs when i

I want my pre('save') mongoose function to operate only once

落花浮王杯 提交于 2021-02-05 10:28:17
问题 I do not know if the exact request in title is possible, but if not; i would really appreciate an alternate solution. I have this pre save method of mongoose ownerSchema.pre("save", function(next) { const owner = this; bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(owner.password, salt, function(err, hash) { // Store hash in your password DB. owner.password = hash; next(); }); }); }); When i save new user(owner) a hash is created successfully and all is good> The problem occurs when i

Node js Error: Mongoose with MondoDB connection string

蓝咒 提交于 2021-01-29 20:14:43
问题 after seting my connections const mongoose = require('mongoose') const Post = require('./database/models/Post') mongoose.connect("mongodb://localhost/testdb", {useNewUrlParser: "true", useUnifiedTopology: true}) and my server page as below: const mongoose = require('mongoose') const app = new express() //mongoose.connect('mongodb://localhost/node-js-blog') //mongoose.connect("mongodb://localhost:27017/node-js-blog", {useNewUrlParser: "true",}) mongoose.connect('mongodb://localhost/node-js

pre save middleware in mongoose

左心房为你撑大大i 提交于 2021-01-29 16:20:31
问题 i am first time using pre save middleware and getting a bit confusion in it. It runs perfectly fine and also my save method is getting executed eventhough i am not calling the next() case 1 tourSchema.pre('save', function () { console.log('first middleware is getting called'); }) But when i do like this when next is declared inside the function params but i don't call the next() it hangs there and the save method is not getting executed case 2 tourSchema.pre('save', function (next) { console

Model.find() returns null after model.save() in Mongoose + Nodejs

寵の児 提交于 2021-01-28 06:43:13
问题 The save function works perfectly but, the find function just below it returns null on searching the save object which is previously saved. Here is the code: var mongoose = require("mongoose"); var User = mongoose.model("User"); module.exports.register = function(req, res) { var user = new User(); user.name = req.body.name; user.email = req.body.email; user.setPassword(req.body.password); user.save(function(err) { var token; token = user.generateJwt(); res.status(200); res.json({ token: token

Mongoose schema option change doesn't reflect in database

陌路散爱 提交于 2021-01-28 06:17:50
问题 I have a mongoose schema defined like the following one: { Username: { type: String, unique: true, required: true, }, Password: { type: String, required: true, minlength: 10, maxlength: 20 } } For example after we launch the production build of our app which is running live, if I want to change "unique" to unique: false for the "Username" how should I do it? So on my machine when the server is running i created a User with Username and Password, mongo created User for me, now i changed the

When exactly does Mongoose's .pre('init') get called?

佐手、 提交于 2020-12-01 12:15:04
问题 I want to create 'games' which each have their own unique access 'code'. The code is required in the schema, and I need to generate a code each time a new game is created. I thought schema.pre('init') would be a good place to generate this access code: GameSchema.pre('init', function(next) { // Code generation logic happens here this.code = myNewlyGeneratedCode next() } Unfortunately, this returns an error message: ValidationError: Game validation failed: code: Path 'code' is required. Why

When exactly does Mongoose's .pre('init') get called?

老子叫甜甜 提交于 2020-12-01 12:14:10
问题 I want to create 'games' which each have their own unique access 'code'. The code is required in the schema, and I need to generate a code each time a new game is created. I thought schema.pre('init') would be a good place to generate this access code: GameSchema.pre('init', function(next) { // Code generation logic happens here this.code = myNewlyGeneratedCode next() } Unfortunately, this returns an error message: ValidationError: Game validation failed: code: Path 'code' is required. Why