gpt4 book ai didi

javascript - 从两个模型返回数据

转载 作者:行者123 更新时间:2023-11-30 00:10:39 24 4
gpt4 key购买 nike

Hello, guys. Please help me. How I can get 'topics' array and append it to res.view(). I tried many variants. But always in 'topics' I got 'undefind'.

<小时/>
Subjects.query("SELECT `subjects`.`id`, `subjects`.`name` AS `subject`  FROM `subjects`  
WHERE `subjects`.`faculty` = " + faculty_id, function (err, subs)
{
subs.forEach(function(subject, topics)
{
var topics = new Array();
Topics.query("SELECT `topics`.`id`, `topics`.`name` AS `topic` FROM `topics`
WHERE `topics`.`subject` = " + subject.id, function (err, tops)
{
topics[subject.id] = tops;
});
console.log(topics);
});
res.view({
title: 'Private cabinet',
users: user[0],
subjects: subs,
});
});

最佳答案

我尝试了很多变体,但只有这个有效:

var async  = require('async');
_ = require('lodash');
module.exports = {
'cabinet': function (req, res)
{
async.auto(
{
user: function(cb)
{
User.query("SELECT `users`.`email`, `users`.`id`, `users`.`name`, `users`.`surname`, `users`.`lastname`, `groups`.`name` as `group`, `faculties`.`name` as `faculty`, `faculties`.`id` AS `faculty_id` FROM `users`, `groups`, `faculties` WHERE `users`.`id` = " + req.session.user + " AND `users`.`group` = `groups`.`id` AND `faculties`.`id` = `groups`.`faculty` GROUP BY `users`.`id`", cb);
},

subjects: ['user', function(cb, async_data)
{
var faculty = async_data.user.map(function (item){return item.faculty_id});
Subjects.find()
.where({faculty: faculty})
.exec(cb);
}],

topics: ['subjects', function(cb, async_data)
{
var subject = async_data.subjects.map(function (item){return item.id});
Topics.find()
.where({subject: subject})
.exec(cb);
}]
},
function allDone (err, async_data)
{
if (err) return res.serverError(err);
var user = async_data.user;
var subjects = async_data.subjects;
var topics = async_data.topics;
_.map(user, function (user) {
var theseSubjects =
_.where(subjects, {faculty: user.faculty_id });
subjects = theseSubjects;
_.map(subjects, function (subject) {
var theseTopics =
_.where(topics, {subject: subject.id });
subject.topics = theseTopics;
});
});
res.view({
title: 'Личный кабинет',
user: user[0],
subjects: subjects
});
});
},

关于javascript - 从两个模型返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24097889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com