.findOne / .findById keep running if no result is found - javascript

I face an issue that i can't resolve alone.
I have a MongoDB collection, in this collection i have 1 document atm.
When i use .findById(_id) or .findOne({_id : id}) with the right _id, everything works.
When i use .findById(_id) or .findOne({_id : id}) with the wrong _id (for test purposes), i have no response (no undefined, no null, nothing) from the DB and my request keep running.
Ty for your time, take care !
EDIT :
Document :
export interface OrderDocument extends mongoose.Document {
user: UserDocument['_id'];
asset_bought: AssetDocument['_id'];
asset_b_symbol: string;
asset_sold: AssetDocument['_id'];
asset_s_symbol: string;
exchange: ExchangeDocument['_id'];
exchange_name: string;
is_draft: Boolean;
amount: number;
atm_price: number;
date: Date;
}
Collection's schema :
const orderSchema = new mongoose.Schema(
{
_id: { type: mongoose.Schema.Types.ObjectId },
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users',
required: true,
},
asset_bought: {
type: mongoose.Schema.Types.ObjectId,
ref: 'assets',
required: true,
},
asset_b_symbol: {
type: String,
required: true,
},
asset_sold: {
type: mongoose.Schema.Types.ObjectId,
ref: 'assets',
required: true,
},
asset_s_symbol: {
type: String,
required: true,
},
exchange: {
type: mongoose.Schema.Types.ObjectId,
ref: 'exchanges',
required: true,
},
exchange_name: {
type: String,
required: true,
},
is_draft: { type: Boolean, default: false },
amount: { type: Number, required: true },
atm_price: { type: Number, required: true },
date: { type: Date, required: true },
},
{ timestamps: true }
);
Service
export async function findAndPopulateOrders(
searchType: 'id' | 'one' | 'many',
query: FilterQuery<OrderDocument>,
_collections: Array<string>
) {
const collections = _collections.join(' ');
if (searchType === 'id') {
return await OrderModel.findById(query).populate(collections);
} else if (searchType === 'one') {
return await OrderModel.findOne(query).populate(collections);
} else if (searchType === 'many') {
return await OrderModel.find(query).populate(collections);
} else {
return {};
}
}

Related

Fetching documents from DB using a prop which is an object id

I get no results, and I don't know why. (DB has documents with this owner id)
As you can see, I've tried using Types.ObjectId but no success yet.
export const getStores = async (
{ owner, platform }: {
owner: string
platform?: string
}): Promise<StoreMainInfo[]> => {
console.log('owner', owner); // owner 62210e86f36af71f58022971
const stores = await StoreModel.aggregate([
{
'$project': {
'_id': 1,
'platform': 1,
'name': 1,
'category': 1,
'logo': 1,
'urls': 1,
'stats': 1,
}
}, {
$match: { owner: Types.ObjectId(owner) }
},
]);
if (!stores.length) {
throw ApiError.BadRequest('Stores not found.');
}
return stores;
};
// Model:
const StoreSchema: Schema = new Schema({
owner: { type: Types.ObjectId, ref: CollectionNames.user, required: true },
platform: { type: String, required: true },
name: { type: String, required: true },
category: { type: String, required: false },
logo: { type: LogoSchema, required: false },
urls: { type: UrlsSchema, required: true },
stats: { type: StatsSchema, required: true },
suppliers: { type: [SupplierSchema], required: true },
})
export default model<Document & Store>(CollectionNames.store, StoreSchema)

I can't figure out how to populate dynamic reference in mongoose

This is my user model code. I am referencing the favorites using dynamic referencing as there are three types of posts that can be added to favorites
import mongoose from 'mongoose'
import bcrypt from 'bcryptjs'
const userSchema = mongoose.Schema(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
required: true,
default: false,
},
contact:{
type:Number,
required: true,
unique:true
},
cnic: {
type: Number,
required: true,
unique: true
},
favorites: [
{
postType: {
type: String
},
postId: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'postType'
},
}
],
itemsRented: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Rent',
}
],
itemsRentedOut: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Rent',
}
],
collectionRequestsSent: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'CommunityService',
}
],
itemsCollected: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'CommunityService',
}
],
servicesOrdered: [
{
type:mongoose.Schema.Types.ObjectId,
ref: 'Services'
}
],
paymentDetails: {
card: { type: Number },
cvc: { type: Number },
name: { type: String },
email: {type: String },
expiryDate: {
month: { type: Number },
year: { type: Number }
},
},
address: { type: String },
isDisputeResolutionStaff: {
type: String,
default: false
}
},
{
timestamps: true,
}
)
userSchema.methods.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password)
}
userSchema.pre('save', async function (next) {
if (!this.isModified('password')) {
next()
}
const salt = await bcrypt.genSalt(10)
this.password = await bcrypt.hash(this.password, salt)
})
const User = mongoose.model('User', userSchema)
export default User
This is my controller. I want to get the posts' details added to favorites by using populate but when I run the code it returns the user object not the post details
const getFavorites = asyncHandler(async(req,res) => {
await User.
findById("61b51adfb7b8a64fd87420d3").
populate("favorites").
exec(function (err, story) {
if (err) throw new Error(err);
console.log(story);
});
})

What's the difference between document.property and document.get('property')?

I have a mongoose document that has timestamps option enabled. I want to make decisions based on this timestamps but I noticed something weird according to my understanding.
I tried to get those values the traditional way (document.createdAt) but that returns undefined. But if I use document.get('createdAt') the value comes as in the database. The docs don't say anything about this. My question is: ¿Why timestamps behave this way?
Edit
The schema I'm using has an array of embedded schemas:
const Customer = new mongoose.Schema({
roles: {
type: [{
type: String,
enum: 'app b2b iot'.split(' '),
}],
default: 'app',
set: (value = []) => (value.includes('app')
? value
: value.concat('app')),
},
email: {
address: {
type: String,
trim: true,
lowercase: true,
set(email) {
this._previousEmail = this.email.address
return email
},
},
verified: {
type: Boolean,
},
token: String,
},
nickname: {
type: String,
trim: true,
},
recoveryToken: String,
gender: String,
birthday: String,
lastLogin: Date,
isAnonymous: {
type: Boolean,
default: false,
},
devices: [Device],
});
Device schema:
const Device = new mongoose.Schema({
customer: {
type: ObjectId,
ref: 'Customer',
required: true,
},
handle: {
type: String,
},
platform: {
type: String,
required: true,
set: toLowerCase,
},
info: Mixed,
smartFilterTags: [{
type: String,
}],
paidUntil: Date,
nh: {
tier: String,
_id: {
type: ObjectId,
},
location: {
type: {
type: String,
enum: ['Point'],
default: 'Point',
},
coordinates: [{
type: Number,
}],
},
})
I have a base plugin that apply when I compile models:
function basePlugin(schema) {
schema.add({
archivedAt: Date,
})
schema.set('timestamps', true)
schema.set('toJSON', {
virtuals: true,
})
schema.set('toObject', {
virtuals: true,
})
}

Mongoose model schema referencing not working - Ecommerce model

I am creating multi vendor ecommerce platform, with the following schema.
var user = new Schema(
{
uid: { type: String, index: true, unique: true },
firstName: { type: String, required: true, default: null },
lastName: { type: String, default: null, default: null },
userEmail: { type: String, unique: true, required: true, lowercase: true, },
userProfileImg: { type: String, required: true, default: null },
userDesignation: { type: String, default: null },
userMobile: { type: Number, required: true, default: null },
products: { type: Schema.Types.ObjectId, ref: 'Product' },
}
);
var product = new Schema(
{
sku: { type: String, required: true, unique: true },
title: { type: String, required: true },
category: { type: Array, default: [] },
images: { type: Array, default: [], },
groups: { type: Array, default: [], },
price: { type: Number, default: null, },
unit: { type: String, default: null, },
quantity: { type: Number, default: null, },
description: { type: String, default: null, },
},
);
var AllUser = mongoose.model('User', user, 'AllUsers');
var Allproducts = mongoose.model('Product', product, 'AllProducts');
how can i save multiple products while referring to multiple users? Later i want to populate products based on the users.
Your problem is in referencing the collection. In here when you compile your models
var AllUser = mongoose.model('User', user, 'AllUsers');
var Allproducts = mongoose.model('Product', product, 'AllProducts');
you use Product and for database collection you use AllProducts. That's the problem so...try doing it like this
var Users = mongoose.model('Users', user, 'Users');
var Products = mongoose.model('Products', product, 'Products');
Give it a proper naming convention.
Also there is s typo here in this code.. here I have fixed it
var product = new Schema(
{
sku: { type: String, required: true, unique: true },
title: { type: String, required: true },
category: { type: Array, default: [] },
images: { type: Array, default: [] },
groups: { type: Array, default: [] },
price: { type: Number, default: null },
unit: { type: String, default: null },
quantity: { type: Number, default: null },
description: { type: String, default: null}
}
);
also in your user schema
var user = new Schema(
{
uid: { type: String, index: true, unique: true },
firstName: { type: String, required: true, default: null },
lastName: { type: String, default: null, default: null },
userEmail: { type: String, unique: true, required: true, lowercase: true,
},
userProfileImg: { type: String, required: true, default: null },
userDesignation: { type: String, default: null },
userMobile: { type: Number, required: true, default: null },
products: [{ type: Schema.Types.ObjectId, ref: 'Product' }]
}
);
make products as an array type so that you can store multiple product ids

Populate in nested schema Mongoose

I have this User model:
const userSchema = new Schema({
_id: {
type: Schema.Types.ObjectId,
required: true
},
name: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
notification: {
experiment_id: {
type: Schema.Types.ObjectId,
ref: "Experiment",
required: false
},
seen: {
type: Boolean,
required: true,
default: false
}
}
});
And this Experiment model:
const experimentSchema = new Schema(
{
_id: {
type: Schema.Types.ObjectId,
required: true
},
name: {
type: String,
required: true
},
description: {
type: String,
required: true,
default: "No Description"
},
author_id: {
type: Schema.Types.ObjectId,
ref: "User",
required: true
}
);
I am trying to populate from User the experiment_id in notification.
And from this populate, I would like to populate the author_id as well.
I have seen some code like I have done below but I didn't succeed.
I am trying this:
User.find(
{
_id: req.params.currentUserId
},
"notification"
)
.populate({ path: "experiment_id", populate: { path: "author_id" } })
.exec((err, notif) => {
});
I fixed it by adding notification.experiment_id in the path
User.find(
{
_id: req.params.currentUserId
},
"notification"
)
.populate({ path: "notification.experiment_id", populate: { path: "author_id" } })
.exec((err, notif) => {
});

Categories

Resources