I have 2 collections setup as below, Dates and Streets.
What I would like to achieve is to, query Streets by a param StreetName and look that up to find it's unique ID and then query the other collection by that ID to pull back all the dates that match.
My route is set up to /wasteDate/:StreetName. Here's what I have:
model.js
var DateSchema = new Schema({
date: {
type: Date
},
street_id: {
type: String,
}
});
var StreetSchema = new Schema({
name: {
type: String
}
});
routes.js
module.exports = function(app) {
var wasteCollections = require('../controllers/wasteController');
app.route('/wasteDate/:streetName')
.get(wasteCollections.get_dates_by_street_name);
};
controller.js
var mongoose = require('mongoose'),
ColDate = mongoose.model('Dates'),
that = this,
Street = mongoose.model('Streets');
(...)
exports.manual_get_dates_by_street = function (id) {
var wasteDates = ColDate.find({ street_id: id }).lean();
return wasteDates;
};
exports.get_dates_by_street_name = function (req, res) {
Street.find({
name: req.params.streetName
}, function(err, street) {
var query;
var theStreetId = street[0].id;
if (err) res.send(err);
query = that.manual_get_dates_by_street(theStreetId);
res.json(query);
});
};
at the moment i'm getting a circular reference error on the JSON.
I don't think I'm doing it the right way and think I may need to amend my schema?
Any help appreciated
You can either use (1) find twice or (2) aggregation.
Here's the first way:
exports.manual_get_dates_by_street = function (id, callback) {
// you are dealing with asynchronous operations, so you have to wait for the callback
// to execute before you can get the data
ColDate.find({ street_id: id }).lean().exec(callback);
};
exports.get_dates_by_street_name = function (req, res) {
// you are expecting one result, so use findOne instead of find
Street.findOne({ name: req.params.streetName }, function (err, street) {
// make sure you handle errors properly such as stopping execution of
// the next lines or else you may get unexpected errors
if (err)
return res.send(err);
// we pass a callback that will be executed once results (or an error) are found
that.manual_get_dates_by_street(street._id, function (err, dates) {
res.json({ dates: dates });
});
});
};
I never used it but I think mongoose-models may resolve your problem. https://github.com/SportZing/mongoose-models
Another possible approach is to put the second query function as a callback of the first.
Related
My .find function is not working. It is not calling my database and returning an error of:
TypeError: beerSchema.findOne is not a function
My other pages display fine, it is just the page, I am trying to load the database on.
app.get("/beers", function(req, res){
//Get all beers
beerSchema.find({}, function(err, beers){
if (err) {
console.log(err);
} else {
res.render("beers", {beers: beers});
}
});
});
BeerSchema:
var beerSchema = new mongoose.Schema({
name:String,
abv: Number,
type:String,
Brewery:String,
Image:String
});
var beer = mongoose.model("Beer", beerSchema );
I get the error that .find is not a function.
You need the model not the schema. Wherever you're making your schema, change this:
module.exports = beerSchema;
To this:
module.exports = mongoose.model("Beer", beerSchema);
Then import this in your app file:
const { Beer } = require("./beer.js");
And use Beer:
Beer.find({}, function(err, beers) {...});
Side note: ES6 shorthand property notation means instead of this:
res.render("beers", { beers: beers });
You can just do:
res.render("beers", { beers });
Hello I'm stuck in my first callback "selectArticleByTitle(title, callback)", the terminal send "Cannot read property 'id' of undefined". I don't know how to force the first callback to finish this and launch the others.
router.get('/article/:title', function(req, res){
dataBase.selectArticleByTitle(req.params.title, function(db_titleERR, db_titleResults){
console.log(db_titleResults);
dataBase.selectArticle(db_titleResults[0].id, function(db_resultsArticleERR, db_resultsArticle) {
//Get id of the previous article
dataBase.previousArticle(db_titleResults[0].id, function(db_previousIdERR, db_previousId){
//Get id of the next article
dataBase.nextArticle(db_titleResults[0].id, function(db_nextIdERR, db_nextId){
//Get lastArticle
dataBase.lastArticle(function(db_lastArticleERR, db_lastArticle) {
});
});
});
});
});
});
});
exports.selectArticleByTitle = function(title, callback){
connection.query('select * from article where title=?', [title], function(err, row){
if(err)
callback(err, null);
else{
if(row){
callback(null, row);
}
}
});
}
Here the log
console.log(db_titleResults);
[ RowDataPacket {
id: 7,
genre: 'Sciences',
picture: 'xw',
source: 'xswx',
title: 'zzazzaz',
meta: 'azazadsq',
inputDate: 2017-04-15T10:00:00.000Z,
visitor: 0 } ]
[]
Thank you in advance
If you want to stick with the original code then try the below...
The issue is that you are being returned one row. However you are trying to access the result as if there are many rows being returned in an array.
The below should at least get rid of your error. I would recommend to check the length of the results as well. if db_titleResults.length is defined then you know sql returned an array.
Instead of db_titleResults[0].id, you should use db_titleResults.id.
router.get('/article/:title', function(req, res){
dataBase.selectArticleByTitle(req.params.title, function(db_titleERR, db_titleResults){
console.log(db_titleResults);
dataBase.selectArticle(db_titleResults.id, function(db_resultsArticleERR, db_resultsArticle) {
//Get id of the previous article
dataBase.previousArticle(db_titleResults.id, function(db_previousIdERR, db_previousId){
//Get id of the next article
dataBase.nextArticle(db_titleResults.id, function(db_nextIdERR, db_nextId){
//Get lastArticle
dataBase.lastArticle(function(db_lastArticleERR, db_lastArticle) {
});
});
});
});
});
});
});
I am not sure what library you are using to connect to sql but can avoid nested call backs with an approach like below:
const sql = require('mssql')
sql.connect(config, err => {
// ... error checks
const request = new sql.Request()
request.stream = true // You can set streaming differently for each request
request.query('select * from article where title=?', [title])
request.on('row', row => {
// Emitted for each row in a recordset
dataBase.selectArticle(row.id, ...);
dataBase.previousArticle(row.id, ...);
dataBase.lastArticle(row.id, ...);
});
request.on('error', err => {
// May be emitted multiple times
});
});
My query is:
db.Room.find({}, function (err, rooms) {
if (!err) {
async.each(rooms, function (room, done) {
db.User.find({}, function (err, users) {
if (err) {
done(err)
} else {
room.users = users
room._name = room.name
done()
}
})
}, function (err) {
if (!err) {
socket.emit('read:rooms', rooms)
}
})
}
})
and schemas are:
room:
var mongoose = require('mongoose')
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var Room = new Schema({
name: String
});
module.exports = Room
user:
var mongoose = require('mongoose')
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var User = new Schema({
email: String
});
module.exports = User
but in front-end:
socket.on('read:rooms', function (rooms) {
$scope.rooms = rooms
})
but rooms has no users property, help me, please
It's because the Rooms schema doesn't have a users property.
So, there are a few ways to fix it. Since it looks like though you want the users property to be something that really isn't part of the schema, and is a client-side join rather than work that is done on the database (which is a good!), I'd suggest you convert the data to be just plain old JavaScript objects when you send it over the socket (this would have happened anyway, you're just doing it a bit earlier).
So, when the find returns, it is actually returning a fully-realized MongooseJS model object. While you can set dynamic properties on the object instance, they aren't part of the "data" of the model, so that when it is serialized later to the client, only the properties that are documented will be available.
So, here is an example of what I'd suggest:
db.Room.find({}, function (err, rooms) {
if (!err) {
var oRooms = [];
async.each(rooms, function (room, done) {
/* etc. your code */
} else {
var oRoom = room.toObject();
oRoom.users = users
oRoom._name = room.name
oRooms.push(oRoom);
done()
}
This technique would use the toObject functionality of a Model to return a JavaScript object (not a Mongoose model). That way, you can do what you'd like to the object, including adding new properties dynamically.
Later, of course, make sure you send the new array of rooms to the client:
if (!err) {
socket.emit('read:rooms', oRooms)
}
I need to develop a module for getting data from mongodb, I already have other models working in my application, but this one does not, this is my schemas:
var mongoose = require('mongoose');
var ProvinciaSchema = new mongoose.Schema({
"nome":String,
"tc_provincia_id":Number,
"id" : Number,
"codice_regione" : Number,
"codice" : Number,
"sigla" : Number
},{collection:'province'})
module.exports = ProvinciaSchema;
this is my model:
var mongoose = require('mongoose');
var ProvinciaSchema = require('../schemas/provincia');
var Provincia = mongoose.model('provincia', ProvinciaSchema);
module.exports = Provincia;
This is how I use the model:
var Provincia = require('../../models/provincia');
Provincia.find({},next( err, province){
if (err){console.log('errorre whoosh '+err);
return next(err,province)
}
if (!province){console.log('trovato nulla')}
console.log('callback tc_istat_id')
return next(err,province)
})
where
next =function(err,prov){
t.equivalent(out,expect)
t.end()
when I launch the test if the condition argument is correct, the execution stuck at Provincia.find and the callback function it is not executed, if I put a wrong condition the section of the code of if(err) is executed, I think there is a problem with my schemas abnd models, but I do not understand what.
function myFunction(callback) {
Provincia.find({},next( err, province){
if (err) {
console.log('errorre whoosh '+err);
return next(err,province)
}
if (!province){
console.log('trovato nulla');
return false;
}
console.log('callback tc_istat_id')
callback(err,province)
})
}
myFunction(function(err,prov){
t.equivalent(out,expect)
t.end()
});
I had no way of testing this but it is how I would set up my callback function. Let me know what you log, and what errors your get if this does not work
I use NodeJS to insert documents in MongoDB. Using collection.insert I can insert a document into database like in this code:
// ...
collection.insert(objectToInsert, function(err){
if (err) return;
// Object inserted successfully.
var objectId; // = ???
});
// ...
How can I get the _id of inserted object?
Is there any way to get the _id without getting latest object inserted _id?
Supposing that in same time a lot of people access the database, I can't be sure that the latest id is the id of object inserted.
A shorter way than using second parameter for the callback of collection.insert would be using objectToInsert._id that returns the _id (inside of the callback function, supposing it was a successful operation).
The Mongo driver for NodeJS appends the _id field to the original object reference, so it's easy to get the inserted id using the original object:
collection.insert(objectToInsert, function(err){
if (err) return;
// Object inserted successfully.
var objectId = objectToInsert._id; // this will return the id of object inserted
});
There is a second parameter for the callback for collection.insert that will return the doc or docs inserted, which should have _ids.
Try:
collection.insert(objectToInsert, function(err,docsInserted){
console.log(docsInserted);
});
and check the console to see what I mean.
As ktretyak said, to get inserted document's ID best way is to use insertedId property on result object. In my case result._id didn't work so I had to use following:
db.collection("collection-name")
.insertOne(document)
.then(result => {
console.log(result.insertedId);
})
.catch(err => {
// handle error
});
It's the same thing if you use callbacks.
I actually did a console.log() for the second parameter in the callback function for insert. There is actually a lot of information returned apart from the inserted object itself. So the code below explains how you can access it's id.
collection.insert(objToInsert, function (err, result){
if(err)console.log(err);
else {
console.log(result["ops"][0]["_id"]);
// The above statement will output the id of the
// inserted object
}
});
if you want to take "_id" use simpley
result.insertedId.toString()
// toString will convert from hex
Mongo sends the complete document as a callbackobject so you can simply get it from there only.
for example
collection.save(function(err,room){
var newRoomId = room._id;
});
You could use async functions to get _id field automatically without manipulating data object:
async function save() {
const data = {
name: "John"
}
await db.collection('users').insertOne(data)
return data
}
Returns (data object):
{
_id: '5dbff150b407cc129ab571ca',
name: 'John',
}
Now you can use insertOne method and in promise's result.insertedId
#JSideris, sample code for getting insertedId.
db.collection(COLLECTION).insertOne(data, (err, result) => {
if (err)
return err;
else
return result.insertedId;
});
Similar to other responses, you can grab the variable using async await, es6+ features.
const insertData = async (data) => {
const { ops } = await db.collection('collection').insertOne(data)
console.log(ops[0]._id)
}
Another way to do it in async function :
const express = require('express')
const path = require('path')
const db = require(path.join(__dirname, '../database/config')).db;
const router = express.Router()
// Create.R.U.D
router.post('/new-order', async function (req, res, next) {
// security check
if (Object.keys(req.body).length === 0) {
res.status(404).send({
msg: "Error",
code: 404
});
return;
}
try {
// operations
let orderNumber = await db.collection('orders').countDocuments()
let number = orderNumber + 1
let order = {
number: number,
customer: req.body.customer,
products: req.body.products,
totalProducts: req.body.totalProducts,
totalCost: req.body.totalCost,
type: req.body.type,
time: req.body.time,
date: req.body.date,
timeStamp: Date.now(),
}
if (req.body.direction) {
order.direction = req.body.direction
}
if (req.body.specialRequests) {
order.specialRequests = req.body.specialRequests
}
// Here newOrder will store some informations in result of this process.
// You can find the inserted id and some informations there too.
let newOrder = await db.collection('orders').insertOne({...order})
if (newOrder) {
// MARK: Server response
res.status(201).send({
msg: `Order N°${number} created : id[${newOrder.insertedId}]`,
code: 201
});
} else {
// MARK: Server response
res.status(404).send({
msg: `Order N°${number} not created`,
code: 404
});
}
} catch (e) {
print(e)
return
}
})
// C.Read.U.D
// C.R.Update.D
// C.R.U.Delete
module.exports = router;