【发布时间】:2017-06-18 13:07:13
【问题描述】:
这可能是一个愚蠢的问题,但学校里没有人知道该怎么做。
我有这段代码可以完美运行(它发送对象表potentials,我可以在客户端使用它)但我记得我还需要另一个(userData)。
所以我尝试将其声明为const,然后在我的res.render 之前将其记录下来,但它是undefined。到目前为止,我们的学生组对 Promise 还不是很熟悉,所以也许我们在这里遗漏了一些东西。
无论如何,这是我的代码,有什么帮助吗?谢谢你。
function matchaSearch (pool, username) {
return suggestUsers(pool, username)
.then((searcherInfos) => {
userData = [...searcherInfos]
if (userData[0].sex === 'm' && userData[0].orientation === 's') {
return lookForSF(pool, username)
} else if ((userData[0].sex) && userData[0].orientation === 'b') {
return lookForbothCauseImB(pool, username)
} else if ((userData[0].sex) === 'f' && userData[0].orientation === 's') {
return lookForSM(pool, username)
} else if ((userData[0].sex) === 'm' && userData[0].orientation === 'g') {
return lookForGM(pool, username)
} else if ((userData[0].sex) === 'f' && userData[0].orientation === 'g') {
return lookforGF(pool, username)
}
})
.then((rows) => {
var potentials = rows;
return (potentials)
})
}
router.post('/matchaSearch', function (req, res) {
const userData = []
matchaSearch(pool, session.uniqueID)
.then((potentials) => {
console.log(userData);
res.render('./userMatch', {potentials, userData})
})
.catch((err) => {
console.error('error', err)
res.status(500).send("we don't have any suggestions for you so far")
})
})
【问题讨论】:
-
userData是空数组吗? -
其实我刚刚找到了答案。很抱歉发布这个。我可以在 res.render 之前从变量中调用我的函数,然后移动它。
-
您的问题得到解答了吗?
标签: javascript node.js express promise pug