I've come across a blocker on attempting to promisify a mongoose method.
As far as my understanding goes I should be able to promisify fn's that take in callbacks with an error and a parameter, but in this case I get this error:
TypeError: this.Query is not a constructor
model code:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = mongoose.model('User', new Schema({
name: String,
password: String,
admin: Boolean
}));
breaking code:
var User = require('./app/models/user');
var { promisify } = require('util');
var findUserPromise = promisify(User.findOne);
...
findUserPromise({ name: req.body.name })
.then(user => console.log("do something with the user"))
.catch(err => { console.log("err ", err) });
Any help is very much appreciated!
mongoose is already very promise-friendly. To get a promise from findOne(), you just need to call .exec():
Instead of
var findUserPromise = promisify(User.findOne);
...
findUserPromise({ name: req.body.name })
.then(user =>
...
Just call it with .exec()
:
User.findOne({ name: req.body.name }).exec()
.then(user =>
...
Yes as #Jim B answer, mongoose is promise friendly. you can also use async and await
const User = require('./app/models/user');
module.export = {
userDetails: async (req, res, next) => {
try {
const user = await User.findOne({ name: req.body.name });
console.log(user);
}
catch(err) {
console.log(err);
}
}
}
Related
file user.js
const mongoose = require("mongoose");
const User = new mongoose.Schema({
name: String,
age: Number,
});
module.exports = mongoose.model("user", User);
app.js which I run database and connect
app.post("/createUser", async (req, res) => {
try {
//console.log(req.body);
const myUser = new User(req.body);
const saveUser = await myUser.save();
res.send(myUser);
} catch (err) {
res.status(500).json(err);
}
});
I try to add data in postman:{"name":"Ngan", "age":21}, but it displays fail
I have a little problem to understand that how to solve this error.i am just a beginner to nodejs and mongodb/mongoose.I am creating a component in reactjs to update any particular documents using its user_id which i am passing a params in routes.
there is the code:
const express = require('express');
const mongoose = require('mongoose');
const mongodb = require('mongodb')
const user = require('../schema');
const router = express.Router();
router.get('/:id', function (req, res) {
const userid = {
userid: (req.params.id || '')
}
console.log('getting to be updated data');
user.db1.findOne(userid, function (err, data) {
if (err) throw err
res.send(data)
console.log(data)
});
});
module.exports = router
//here is the user model:
const userSchema = new mongoose.Schema({
userid:{type:String},
fullname:{type:String},
phone:{type:Number},
email:{type:String},
})
const skillSchema = new mongoose.Schema({
userid:{type:Number},
skills:{type:String},
})
const users = mongoose.model('users',userSchema);
const skills = mongoose.model('skills',skillSchema);
module.exports ={
db1 : users,
db2 : skills
}
I guess you haven't specified in the find.one () function by which parameter it will search. Try it by typing your own column name instead of id.
user.db1.findOne({ id: userid } ,function(err, data){
if (err) throw err
res.send(data)
console.log(data)
});
Or you can change your function and use findById().
user.db1.findById(userid, ,function(err, data){
if (err) throw err
res.send(data)
console.log(data)
});
The problem start when I use the bcrypt middleware to encrypt my password.
Whitout bcrypt I could save the users, but with it not now.
My users.js file
'use strict'
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const bcrypt = require('bcrypt')
const UserSchema = new Schema({
email: { type: String, unique: true, lowercase: true },
displayName: String,
password: { type: String, select: false }
})
UserSchema.pre('save', (next) => {
let user = this
if (!user.isModified('password')) {
return next();
}
bcrypt.genSalt(10, (err, salt) => {
if (err) return next(err)
bcrypt.hash(user.password, salt, null, (err, hash) => {
if (err) return next(err)
user.password = hash
next();
})
})
})
module.exports = mongoose.model('User', UserSchema)
My router.js file:
const express = require('express')
const router = express.Router()
const mongoose = require('mongoose')
const User = require('./model/user')
const bcrypt = require('bcrypt')
router.post('/user', (req, res) => {
console.log(req.body)
let user = new User()
user.email = req.body.email
user.displayName = req.body.displayName
user.password = req.body.password
user.save((err, stored) => {
res.status(200).send({
user: stored
})
})
})
This is the server response:
{}
My db is not affected...
I can see two mistakes in the provided code:
1. this in the pre-save middleware is not a user document instance
Arrow functions do not provide their own this binding:
In arrow functions, this retains the value of the enclosing lexical context's this. [source]
Change your code to the following:
UserSchema.pre('save', function (next) {
const user = this;
// ... code omitted
});
2. Not handling duplicate key MongoError
The request might fail with MongoError: E11000 duplicate key error collection since email field is unique. You are ignoring such fail and since stored in your user.save() is undefined the response from the server is going to be an empty object.
To fix this issue you need to add a handler in the following code:
user.save((err, stored) => {
if (err) {
throw err; // some handling
}
res.status(200).send({user: stored});
});
So, here's my user schema where i declared hello method onto the Userschema which im using to test
//user.model.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
trim: true,
minlength: 3
},
password: { type: String, required: true }
});
UserSchema.methods.hello = () => {
console.log("hello from method");
};
const User = mongoose.model("User", UserSchema);
module.exports = User;
here's routes file
//authroutes.js
const router = require("express").Router();
let User = require("../models/user.model");
router.route("/").get((req, res) => {
res.send("auth route");
});
router.route("/signup").post((req, res) => {
const username = req.body.username;
const password = req.body.password;
const newUser = new User({
username,
password
});
newUser
.save()
.then(() => res.json(`${username} added`))
.catch(err => console.log(err));
});
router.route("/login").post(async (req, res) => {
await User.find({ username: req.body.username }, function(err, user) {
if (err) throw err;
//this doesnt work
user.hello();
res.end();
});
});
module.exports = router;
in the login route im calling hello function to test but that doesnt work and throws this error
TypeError: user.hello is not a function
You need to use User.findOne instead of User.find, because find returns an array, but what we need is an instance of the model.
Also Instance methods shouldn't be declared using ES6 arrow functions.
Arrow functions explicitly prevent binding this, so your method will not have access to the document and it will not work.
So you had better to update method like this:
UserSchema.methods.hello = function() {
console.log("hello from method");
};
I'm getting an error running router.patch() code to update a product on a cloud-based mongoose database. I'm using Postman to simulate the update. Postman's error is showing, "req.body[Symbol.iterator] is not a function." Here is the relevant code:
Products.js:
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Product = require('../models/product');
router.patch('/:productId', (req, res, next) => {
const id = req.params.productId;
// don't want to update both fields if not needed
const updateOps = {};
// loop through all the operations of the request body
for (const ops of req.body) {
updateOps[ops.propName] = ops.value;
}
Product.update({_id: id}, { $set: updateOps })// $set is a mongoose object
.exec()
.then(result => {
console.log(result);
res.status(200).json(result);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
{req.body.newName, price, req.body.newPrice} ;
res.status(200).json({
message: 'Updated product',
});
});
module.exports = router
Product.js:
const mongoose = require('mongoose');
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
price: Number
});
// export the schema into the Mongoose model
module.exports = mongoose.model('Product', productSchema);
Any help would be greatly appreciated. :-)
As I commented, this part is likely your problem:
for (const ops of req.body) {
updateOps[ops.propName] = ops.value;
}
Since req.body is an Object, I think you want:
for (let [key, value] of Object.entries(req.body)){
updateOps[key.propName] = value;
}
Or something similar.