The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

503192 (2) [Avatar] Offline
#1
Simon, happy 2018! this has been a great book. I have version 1 but I am using all the latest versions of heroku and moved to pug etc.

I ran through the book using the examples and now am building my real app! Could not have done it without the help of the book. You gave me enough to get me started on so many technologies, I have lost count!

I am hoping I can get some help with an issue i have been wrestling with all day.

I have two collections that I want to effectively join - i.e. get an attribute from a second collection based on an object id stored in the first.

This is what my code looks like. I think this is a basic javascript issue i am having, not necessarily mongo or node.

var sendJsonResponse = function (res, status, content) {
res.status(status);
res.json(content);
};

var mongoose = require("mongoose");
var Coll = mongoose.model("collectible");
var Img = mongoose.model("image");


module.exports.collectiblesList = function (req, res) {
var firstImage_ids = [];
var imageDir = "https://s3.amazonaws.com/xxxxxxxxxxx/images/";
var defaultImage = imageDir + "default.jpg";


// get the collectibles
Coll.find(req.body).exec(function (err, collectibles) {
if (!collectibles) {
// not found
sendJsonResponse(res, 404, {"message": "no collectibles found"});
return;
}
if (err) {
console.log(collectible);
sendJsonResponse(res, 400, err);
return;
}
//found the collectibles
// now find the list of images we need to pull (each colelctible can have multiple images
collectibles.forEach( function(collectible,index) {
if (collectible.image_ids) {
firstImage_ids.push(collectible.image_ids[0]);
collectibles[index].imageUrl = defaultImage; // this is the line that has no effect.
// collectible.imageUrl = defaultImage; // tried this too.
// collectible["imageUrl"] = defaultImage; // and this. smilie
console.log(collectible);
}
});
sendJsonResponse(res, 200, collectibles);
});
};


I can not seem to add a new key/value to the collectibles that were returned by the exec method/callback.

I have yet to code up my 2nd db query to pull the imageUrls from the image collection (plan to use {url: {$in:image_ids}} in my .find method) because any attempt I have made to add a new key/value to the objects in the collectibles.

any ideas what i am doing wrong?

thanks
Glyn





503192 (2) [Avatar] Offline
#2
I worked it out after about 2 days of effort and then managed to find a document explaining the situation:

https://stackoverflow.com/questions/18821212/mongoose-whats-up-with-doc

I think i am not sticking with the principles of MVC and I am putting concerns in the wrong place. ie i am in effect trying to join objects together in the controller that sits behind the REST API. I think that it belongs elsewhere and if the view wants to display data from a related in a related collection, then it should pull that data through the rest api. I have yet to read section 3 of the book - and that is what i am now doing. I think all will become much clearer.

I feel that this book has been brilliant - its such a great, practical guide - empowering.