我试图重用我在两个不同的NodeJS控制器中拥有的代码,但是我似乎还不了解Java调用是如何工作的。数据来自使用Mongoose的MongoDB
我可以单独调用图片代码-findMyBoatPictures很好,但是当我尝试从船控制器代码中调用它时-findMyForSale我的踪迹是
跟踪是通过NodeJS产生的
in boats findMyBoatPictures
picture found records: 3
因此找到了3条记录,但之后它消失在某个地方,我看不到哪里
任何帮助将不胜感激
我想要实现的是,在获得了三个记录之后,我想将每个记录的一部分添加到我的第一条船记录中,这些记录在Boat.find中返回,然后对每个船记录重复此过程< / p>
picture.routes.js
module.exports = function(app) {
const picture = require('../controllers/picture.controller.js');
// Retrieve all by boat Id
app.get('/api/pictures/:boatId', picture.findMyBoatPictures);
}
boat.routes.js
module.exports = function(app) {
const boats = require('../controllers/boat.controller.js');
// Retrieve all Boats for sale
app.get('/api/boatsforsale/:agentId', boats.findMyForSale);
}
boat.controller.js
const Boat = require('../models/boat.model.js');
const picture = require('../controllers/picture.controller.js');
// FETCH all Boats for sale by specified agent
exports.findMyForSale = (req, res) => {
var query = { sellingAgent: "1" }; // fixed to 1 for testing
Boat.find(query)
.then(boats => {
req.params.boatId = boats[0].boatId;
var pics;
picture.findMyBoatPictures(myCall, res, function (pics) {
if (pics) {
console.log("find pics #1 result is " + JSON.stringify(pics));
}
else {
console.log("find pics #2 result is " + JSON.stringify(pics));
}
res.json(boats);
});
}).catch(err => {
console.log("findMyForSale failure is " + JSON.stringify(err));
res.status(500).send({
msg: err.message
});
});
};
picture.controller.js
const Picture = require('../models/picture.model.js');
exports.findMyBoatPictures = (req, res) => {
console.log("in boats findMyBoatPictures")
var query = { owningId: String(req.params.boatId) };
var orderBy = { sequenceNumber: 1 };
Picture.find({ owningId: String(req.params.boatId) }).sort({ sequenceNumber: 1 })
.then(pics => {
console.log("picture found records: " + pics.length);
res.json(pics);
}).catch(err => {
console.log("picture found error " + err.message);
res.status(500).send({
msg: err.message
});
});
};
答案 0 :(得分:1)
问题:
图片控制器功能findMyBoatPictures
用res.json
响应请求。它不会将控件传递回调用它的findMyForSale
方法。
解决方案:
如果您要重用一些代码,请在两个控制器都使用的服务中对其进行抽象。该服务可以使用回调将控制传递回去,然后控制器可以使用彼此独立的res.json
来响应请求。
答案 1 :(得分:0)
首先exports.findMyBoatPictures函数接受两个参数req和res,但是您没有正确启动它,这是您应该做的
picture.findMyBoatPictures(req, res);
如果您打算取回值,则必须使用回调函数,即
const Picture = require('../models/picture.model.js');
exports.findMyBoatPictures = (req, res, callback) => {
console.log("in boats findMyBoatPictures")
var query = { owningId: String(req.params.boatId) };
var orderBy = { sequenceNumber: 1 };
Picture.find({ owningId: String(req.params.boatId)
}).sort({ sequenceNumber: 1 })
.then(pics => {
console.log("picture found records: " + pics.length);
callback(false, pics)
}).catch(err => {
console.log("picture found error " + err.message);
callback (err)
});
那你做
let pics;
picture.findMyBoatPictures(req, res, function (err, pics)
{
if (!err && pics) {
console.log("find pics #1 result is " +
JSON.stringify(pics));
pics = pics;
// Send ur response
}
else {
console.log(err);
// Send ur response
}
});
如果您使用async和await,甚至可以减少代码行数