Schema declaration and subdocument - node.js

Can you tell me what i'm doing wrong ?
var ObjectId = Schema.Types.ObjectId;
var ProductSchema = new Schema({
name: { type: String, required: true },
price: { type: Number, required: true },
category : { type: String, required: true }
});
var OrderSchema = new Schema({
products: [{
product: {type: ObjectId, ref: 'Product'},
quantity: {type: Number}
}],
status: { type: String, required: true }
});
Product = mongoose.model('Product', ProductSchema);
Order = rmongoose.model('Order', OrderSchema);
OrderSchema.statics.addOrder = function (data, cb) {
// data: array of products ID
var newOrder = new Order();
data.data.forEach(function(element, index, array) {
Product.findOne({ '_id': element.id }, function (err, product) {
if (err) return handleError(err);
newOrder.products.push({product: product, quantity: element.quantity});
})
});
newOrder.status = 'waiting';
newOrder.save(function (err, order) {
if (err) cb(err, false);
console.log(order);
var response = json.stringify({
event: 'addOrder',
success: true,
data: order.id
});
cb(false, response);
});
}
When i add an order products, array is always empty but i have no error. Maybe it's the wrong to do what i want.
Data send by the client are good and the foreach and findOne work well but push seems doing nothing.
If there is no solution maybe you can try to help me to find another solution.
Thanks :)

That's because you need to wait for all the products to be found.
Try this (untested):
OrderSchema.statics.addOrder = function (data, cb) {
// data: array of products ID
var newOrder = new Order();
var productIds = [];
var quantity = [];
data.data.forEach(function(element, index, array) {
productIds.push(element.id);
quantity.push(element.quantity);
});
Product.find({ '_id' : { $in: productIds} }, function(err, products) {
if (err) return handleError(err);
products.forEach(function(product, index) {
newOrder.products.push({product: product, quantity: quantity[index]});
});
newOrder.status = 'waiting';
newOrder.save(function (err, order) {
if (err) cb(err, false);
console.log(order);
var response = json.stringify({
event: 'addOrder',
success: true,
data: order.id
});
cb(false, response);
});
});
});

Related

find all in mongoose

I have Schema With name score, and that have an iduser: req.session.user._id.
and I have more than one items with same iduser. I wanna to find all the items with the same iduser. and I use the
var users = await storescors16.find({ id }) id = res.session.user._id.
but that show me all the items in score !
My code
//schema
const storescor = new mongoose.Schema({
iduser: String,
level: { type: String, default: null },
day: { type: String, default: null },
date: { type: String, default: null },
time: { type: String, default: null },
objectif: { type: String, default: null }
})
var storescors16 = mongoose.model("storescor", storescor);
//post infos to database
router.post('/control/edite-control', (req, res) => {
console.log(req.session.selectuserid);
var { level, day, date, time, ob } = req.body
var scor = new storescors16({
iduser: req.session.selectuserid,
level: level,
day: day,
date: date,
time: time,
objectif: ob,
})
//read infos from databse
router.get('/result', auth, async(req, res) => {
var id = req.session.user._id
console.log(id);
var user = User.findById(id, (err, docs) => {
if (err)
console.log(err);
else
console.log();
})
var ids = req.session.user._id
var notes = await storescors16.find({ ids })
console.log(notes);
let scor = user.quiz
res.render('./options/result', {
notes: notes,
scor: scor,
title: 'سجل درجات النجم',
name: session.user.name,
email: session.user.email,
})
});
I use nodejs as backend
If you want to find all use findAll function with where.
const users = await storescors16.findAll({ id });
That if I understand you currect.

Node express find and return response multple models

I'm fairly new to node & express, I'm trying to implement a register application.
I have 2 models, both models have one common field 'empID'.
const RegisterEntriesSchema = mongoose.Schema({
empID: Number,
registerType: String,
registerItemsQuantity: Number,
registerItemsDesc: String
}, {
timestamps: true
});
const RegisterEmpSchema = mongoose.Schema({
empID: Number,
empName: String,
empPhone: String,
empProj:String
}, {
timestamps: true
});
For my get call in which I need to merge the values, I get from RegisterEmpSchema with its corresponding
employee details from RegisterEmpSchema.
exports.findAllRegisterEntries = (req, res) => {
registerEntriesModel.find()
.then(result => {
var updatedResponse=[];
console.log(result[0].empID);
for(var i=0;i<result.length;i++){
registerEmpModel.find({ empID: result[i].empID })
.then(result2 => {
**//unable to access result here**
}).catch(err => {
console.log("exception catch called findAllRegisterEntries, find employee details "+err);
});
}
res.send(updatedResponse);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while retrieving register."
});
});
};
I basically need to get register data and its corresponding employee data.
How do I modify my find() code to use the key empID and do a join query fetch?
I think you better use populate, add ref to empID inside RegisterEntriesSchema
const RegisterEmpSchema = new mongoose.Schema({
empID: Number,
empName: String,
empPhone: String,
empProj: String
}, {
timestamps: true
});
const registerEmpModel = mongoose.model('RegisterEmpSchema', RegisterEmpSchema, 'registerEmployeeCollection');
const RegisterEntriesSchema = new mongoose.Schema({
registerType: String,
registerItemsQuantity: Number,
registerItemsDesc: String,
empID: {
type: mongoose.Schema.Types.ObjectId,
ref: 'RegisterEmpSchema'
}
}, {
timestamps: true
});
RegisterEntriesSchema.index({ createdAt: 1 }, { expires: '525601m' });
const registerEntriesModel = mongoose.model('RegisterEntriesSchema', RegisterEntriesSchema, 'registerEntriesCollection');
module.exports = {
registerEmpModel, registerEntriesModel,
}
then use populate() to populate the RegisterEntriesSchema with correspondence empID
RegisterEntriesSchema.
find().
populate('empID').
exec(function (err, data) {
if (err) return console.log(err);
res.send(data);
});
check mongoose docs: https://mongoosejs.com/docs/populate.html

Mongo db, how to give object _id another collection's document

I have 2 collections called User and Location. In User, there is a location _id and this is an Object. Id also references the location collection. My question is what did I do wrong? When I call getUser function I want to see user information and the user's location information. What I need to do ?
User Schema
module.exports = (function userSchema() {
var Mongoose = require('mongoose');
var userSchema = Mongoose.Schema({
name: {
type: String,
require: true
},
surname: {
type: String,
require: true
},
tel: {
type: String,
require: true
},
age: {
type: String,
require: true
},
mevki_id: {
type: String,
require: true
},
location_id: [{
type: Mongoose.Schema.Types.ObjectId,
ref: 'locations'
}]
});
var collectionName = 'users';
var User = Mongoose.model(collectionName, userSchema);
return User;
})();
User Controller
function userController() {
var User = require('../models/UserSchema');
this.createUser = function (req, res, next) {
var name = req.body.name;
var surname = req.body.surname;
var tel = req.body.tel;
var age = req.body.age;
var mevki_id = req.body.mevki_id;
var lok_id = req.body.lok_id;
User.create({
name: name,
surname: surname,
tel: tel,
age: age,
mevki_id: mevki_id,
lok_id: lok_id
}, function (err, result) {
if (err) {
console.log(err);
return res.send({
'error': err
});
} else {
return res.send({
'result': result,
'status': 'successfully saved'
});
}
});
};
this.getUser = function (req, res, next) {
User.find()
.populate('lok_id')
.exec(function (err, result) {
if (err) {
console.log(err);
return res.send({
'error': err
});
} else {
return res.send({
'USERS': result
});
}
});
};
return this;
};
module.exports = new UserController();
First, your schema is wrong:
var userSchema = new Mongoose.Schema({
// ...
location_id: { type: [Mongoose.Schema.Types.ObjectId], ref: 'locations' }
})
Second, in your schema the last field name is location_id while in your controller, you change it to lok_id.
So, fix this:
User.create({
// ...
location_id: lok_id
}
and this:
User
.find()
.populate('location_id')
UPDATE
In your json the last field name is location_id, therefore, fix this too:
this.createUser = function (req, res, next) {
// ...
var lok_id = req.body.location_id;
}

mongoose update model before saving

I have a model:
const wordSchema = mongoose.Schema({
author: {type: Object, default: 'unknown'},
quote: String,
source: {type: String, default: 'unknown', index: true},
rating: {type: Number, default: 0},
createdAt: {type: Date, default: Date.now},
updatedAt: {type: Date, default: Date.now},
});
Now after receiving a POST request to my server, I want to make a GET request to wikipedia, and get the author info, then append it to my model as an object, and write this model into my database.
app.post('/', function(req, res) {
let author = {};
let quote = new Word({
author: req.body.author,
quote: req.body.quote,
source: req.body.source,
rating: req.body.rating,
});
let authorName = req.body.author.replace(/ /g, '%20');
let url = 'https://en.wikipedia.org/w/api.php?action=query&format=json&titles=' + authorName + '&prop=pageimages|extracts&pithumbsize=200&exsentences=10&exintro=true';
request.get(url, (error, response, body) => {
if(error) {
return error;
}
let data = JSON.parse(body);
let pageID;
for(page in data.query.pages) {
pageID = page;
}
author = {
name: req.body.author,
thumbnail: data.query.pages[pageID].thumbnail.source,
flavorText: data.query.pages[pageID].extract,
};
});
// Save the quote
quote.pre('save', (next) => {
this.author = author;
})
quote.save(function(err, quote) {
if (err) {
res.send(err);
}
res.redirect('/words');
});
});
Now I tried updating the value with the .pre function, but I am getting
quote.pre is not a function
What would be the 'correct way of doing this and what am I doing wrong?
quote is not a Mongoose schema, but wordSchema is so it needs to be as follow:
wordSchema.pre('save', (next) => {
this.quote = whteverYouWantToAssignWith;
})
But actually you don't need it in your use case as far as I understand, you may achieve what you want as follow:
app.post('/', function(req, res) {
let authorName = req.body.author.replace(/ /g, '%20');
let url = 'https://en.wikipedia.org/w/api.php?action=query&format=json&titles=' + authorName + '&prop=pageimages|extracts&pithumbsize=200&exsentences=10&exintro=true';
request.get(url, (error, response, body) => {
if(error) {
return error;
}
let data = JSON.parse(body);
let pageID;
for(page in data.query.pages) {
pageID = page;
}
let quote = new Word({
author: {
name: req.body.author,
thumbnail: data.query.pages[pageID].thumbnail.source,
flavorText: data.query.pages[pageID].extract,
},
quote: req.body.quote,
source: req.body.source,
rating: req.body.rating,
});
quote.save(function(err, quote) {
if (err) {
res.send(err);
}
res.redirect('/words');
});
});
});

Retrieve Documents containing a subdocument in a REST API

I am trying to retrieve a list of documents that contain a sub doc to be listed on a web application.
I have my models setup as such:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var storeSchema = new mongoose.Schema({
name: String,
address: String,
phone: String,
webUrl: String,
coords: {type: [Number], index: '2dsphere'}
});
var reviewSchema = new mongoose.Schema({
user: {type: String, required: true},
store: { type: Schema.ObjectId, ref: 'Store' },
review: {type: String},
tags: [String]
});
mongoose.model('Review', reviewSchema);
mongoose.model('Store', storeSchema);
And the api controller setup as such:
var mongoose = require('mongoose');
var Game = mongoose.model('Review');
var sendJsonResponse = function(res, status, content) {
res.status(status);
res.json(content);
};
module.exports.gamesListByDistance = function(req, res) {
var lng = parseFloat(req.query.lng);
var lat = parseFloat(req.query.lat);
var maxDistance = parseFloat(req.query.maxDistance);
var point = {
type: "Point",
coordinates: [lng, lat]
};
var geoOptions = {
spherical: true,
maxDistance: theEarth.getRadsFromDistance(maxDistance),
num: 10
};
Review.geoNear(point, geoOptions, function(err, results, stats) {
console.log('Geo Results', results);
console.log('Geo stats', stats)
if (err) {
console.log('geoNear error:', err);
sendJsonResponse(res, 404, err);
} else {
results.populate(results, {path:'store', select:'name coords'}, function(err,reviews) {
if (err) {
sendJsonResponse(res, 400, err);
} else {
games = buildReviewsList(req, res, results, stats);
sendJsonResponse(res, 200, reviews);
}
});
}
});
};
var buildReviewsList = function(req, res, results, stats) {
var reviews = [];
results.forEach(function(doc) {
reviews.push({
distance: theEarth.getDistanceFromRads(doc.dis),
store: doc.obj.store.name,
status: doc.obj.status,
tags: doc.obj.tags,
_id: doc.obj._id
});
});
return reviews;
};
But am getting:
TypeError: undefined is not a function
What is the proper way to populate these subdocs and return the list of reviews to be consumed with a Web Application?
Why do you have :
var Game ?
should be var Review.

Resources