Mongoose findOne() callback returning null - javascript

I'm trying to find a user in my node app with mongoose by using
var User = require('../app/models/user');
function mongoTest() {
var publicAddress = "0x8a6be8979340faa30020b0c1f617d8fd4309679f";
User.findOne({"publicAddress": publicAddress}, (err, user) => {
if (err) {
res.status(500).send(err)
} else {
console.log(user);
}
});
}
and err and user always return null. From other questions here (this and this), this usually seems to be related to mongoose pluralising collections. However, I don't think that's my issue because my users.js has
module.exports = mongoose.model('User', userSchema);
// Have also tried module.exports = mongoose.model('User', userSchema, 'User');
For completeness, users.js is
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
// Define the schema for our user model
var userSchema = mongoose.Schema({
local: {
username: String,
password: String,
pictureCaption: String,
publicAddress: String,
contractAddress: String
}
});
Finally, I'm sure that public address exists because I can see it in the mongoDB with Robo 3T.

In your userSchema the publicAddress is part of local object.
var userSchema = mongoose.Schema({
local: {
username: String,
password: String,
pictureCaption: String,
publicAddress: String,
contractAddress: String
}
});
You are trying to find an object with publicAddress but it's actually inside the local object. So you should edit the query as follows to get the result.
User.findOne({"local.publicAddress": publicAddress}, (err, user) => {
if (err) {
res.status(500).send(err)
} else {
console.log(user);
}
});

Related

Reference another Schema in Mongoose

so I have to Schemas. PostSchema and UserSchema
const mongoose = require("mongoose")
const PostSchema = new mongoose.Schema({
content: {
type: String,
required: true,
},
likes: {
type: Number,
required: true
},
rescreams: {
type: Number,
required: true
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
createdAt: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model("Post", PostSchema)
UserSchema:
const bcrypt = require("bcrypt");
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
userName: { type: String, unique: true },
email: { type: String, unique: true },
password: String,
});
// Password hash middleware.
UserSchema.pre("save", function save(next) {
const user = this;
if (!user.isModified("password")) {
return next();
}
bcrypt.genSalt(10, (err, salt) => {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
});
// Helper method for validating user's password.
UserSchema.methods.comparePassword = function comparePassword(
candidatePassword,
cb
) {
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
cb(err, isMatch);
});
};
module.exports = mongoose.model("User", UserSchema);
My question is: I'm trying to reference the User Object ID in the Post Schema. As you can see, I've done that with type: mongoose.Schema.Types.ObjectID. And I've seen this multiple times. But in my database, the User never shows up in the Document. What do I need to do?
Cheers
There is a difference between referencing a document and embedding a document.
If you want to store a document inside a document you should embed it, thus read operations will be faster because you won't need to perform JOIN operations.
Whereas referencing means storing an ID of an entity that you are referencing, when you need to access the document you are referencing, you need to fetch it from the collection by the ID you have stored. It is slower than embedding, but it gives you higher consistency and data integrity because the data is stored once at the collection and not duplicated at every object. And MongoDB does not support foreign keys so you should be careful with referencing.
So when you are storing the document using ref, you need to put an ObjectID as a user and then fetch the document you need to add populate call. e.g.
PostShema.findOne({ _id: SomeId }).populate('user');
try to save in a variable:
const UserId = UserSchema.Schema.Types.ObjectId;
for more information:
https://mongoosejs.com/docs/api/schema.html#schema_Schema.Types

Turn String into Mongoose Model

I'm learning how to make Mongoose models in Express right now and was wondering if there is a way to convert a string into a matching model.
For instance, if I have a Mongoose model called "User" and a variable like const name = "User", is there a way to convert the string "User" into a User model? Specifically, is there a way to make the following code work?
const name = 'User';
name.findByIdAndUpdate()
Suppose this was the model:
const schema = new mongoose.Schema({ name: 'string' });
const User = mongoose.model('User', schema);
const name = 'User';
Now use the model to construct a document:
const user = new User({ name });
user.save(function (err) {
if (err) return handleError(err);
// saved!
});
or
User.create({ name }, function (err, user) {
if (err) return handleError(err);
// saved!
});
or
const user = await User.create({ name });
Now you can use:
User.findByIdAndUpdate(user.id, { ... }, { ... });
Reference: https://mongoosejs.com/docs/models.html.

mongoose, node, cannot use the method defined in user.model.js

I am using node and mongoose to build an app. And when testing, there exists a strange problem. Here is my auth.index.js(for user login).
auth.index.js:
var express = require('express');
var mongoose = require('mongoose');
var passport = require('passport');
var config = require('../config/environment');
var User = require('../api/user/user.model');
var jwt = require('jsonwebtoken');
var auth = require('./auth.service');
var router = express.Router();
router.post('/login', function (req, res, next){
User.find({email: req.body.email}, function (err, user){
if (err) {
return next(err);
} else if (!user) {
return res.json({message: 'Invalid email.'});
} else {
if(!user.authenticate(req.body.password)){
return res.json({message: 'Invalid password.'});
};
var token = auth.signToken(user._id, user.username, user.role);
return res.json({token: token});
};
});
});
Here is the user.model.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username: String,
password: String,
email: {type: String, lowercase: true},
role: {type: String, default: 'user'},
provider: String,
date: String
});
UserSchema.methods = {
//check password
authenticate: function(plainText) {
return plainText === this.password;
}
};
However, in the command window, it returns that
user.authenticat is not a function.
I wanna know why, and does it mean that I cannot use the method directly or I need to invoke the method through passport ? Thanks.
I think you have misplaced your methods. It should be like this.
UserSchema.methods.authenticate: function(plainText) {
return plainText === this.password;
}
See the following link for the complete example. https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications

Mongoose save doesn't work "id is not defined"

It's my first time using Mongoose and the save function is not working for me...
On user model file:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = mongoose.model('User', {
name: Schema.Types.Mixed,
gender: String,
email: String
});
module.exports = User;
On my user controller file:
// Create a user
router.post('/', function(req, res) {
var user = new User(req.body);
user._id = mongoose.Types.ObjectId();
user.save(function(err) {
if (err) {
return res.status(500).json({
error: "Error creating user: " + err
});
}
return res.status(200).end();
});
});
I tried everything but I can't save the user object to the database: "id is not defined"
On the db the _id is a ObjectId.
Thanks.
mongoose.model receives collection name and a Schema as arguments, not an regular object.
So you should use the following code instead:
var schema = new mongoose.Schema({
name: Schema.Types.Mixed,
gender: String,
email: String
});
var User = mongoose.model('User', schema);
maybe we can change the code structure && remove "user._id", test please this code :
*model file:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name: Schema.Types.Mixed,
gender: String,
email: String
});
module.exports = mongoose.model('User', UserSchema);
*controller file:
// Create a user
router.post('/', function(req, res) {
var user = new User(req.body);
user.save(function(err) {
if (err) {
return res.status(500).json({
error: "Error creating user: " + err
});
}
return res.status(200).end();
});
});

Multiple connection error when using node js and mongodb

I'm using the following schema located in my /routes/schema.js file...
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
username: String,
password: String,
name: String,
last: String,
year: Number,
degree: String
});
var User = mongoose.model('User', userSchema);
module.exports = {
User: User
}
In my /routes/register.js file I am storing some information using POST data...
var User = require('../routes/schema').User;
exports.postRegister = function (req, res) {
var u = new User({
username: req.body.reg_username,
password: req.body.reg_password,
name: req.body.reg_name,
last: req.body.reg_lastname,
year: req.body.reg_year,
degree: req.body.reg_degree
});
u.save(function (err) {
if (err) {
throw err;
}
else {
console.log("saved");
res.render('index', { title: 'Express' });
}
});
}
Everything gets saved fine in my Database. But now, this register.js file redirects the user back to /routes/index.js where the user must then sign in using some credentials stored in the database.
So in my index.js file I need to check if username and password exist together in a collection in my database, I tried the following...
var User = require('../routes/schema').User;
exports.signin = function (req, res) {
User.findOne({
username: req.body.log_username,
password: req.body.log_password
}, function (err, docs) {
if (docs.length) {
console.log("name exists");
}
else {
console.log("no exist");
}
});
};
I used the findOne function with the same Schema to check if username and password exist in a collection in the database, but it doesn't work properly. I seem to get a multiple connection error and I do not know how to avoid it.
When I try to login using some credentials already in the database, the console prints out no exist meaning the else statement in exports.signin is reached.
The value of the docs will be null if the object is not found in the collection.
If the user enters wrong credentials, you will be calling docs.length on null object, which will cause an error. Use docs != null instead, to avoid calling length on null object.
try it docs.length !== 0 this is check docs field is existed or empty.

Categories