gpt4 book ai didi

javascript - 在express.js中,res.render()不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:45 25 4
gpt4 key购买 nike

我正在制作一个使用 Facebook 登录的简单网络应用程序。

如果我页面上的 Facebook 登录按钮被点击,

FB.api(
'/me',
'GET',
{"fields":"id,name,birthday,gender"},
function(response) {
$.post('fb_login', response, "json");
}
);

被调用,并且路由器处理'/fb_login'请求;在路由器中,服务器检查 json 对象的 id 是否已在其数据库中。如果没有,则应调用 res.render('signup', ...) 。然而它没有用。我已经检查了 res.render() 被调用,但页面“signup.jade”没有显示。

这是我的路由器源代码。

var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '1012'
});

/* GET home page. */
router.post('/', function(req, res, next) {
var userid = req.body.id;
if (userid) {
pool.getConnection(function(err, connection) {
if (err) {
console.error('DB Connection error!!');
return;
}
console.log('DB Connection Success!!');
connection.query('use vasket');
connection.query('select count(*) result from user where userID=?',
[userid], function(err, result, field) {
var isAlreadyUser = result[0].result;
console.log(isAlreadyUser);
if (isAlreadyUser == 1) {
req.session.userid = userid;
res.redirect('/');
res.end();
console.log('DB FB Login Success!!');
connection.release();
}
else {
connection.release();
console.log('FIRST TIME!');
//This method was called, but the page rendered didn't
res.render('signup', {id: req.body.id, name: req.body.name, birthday: req.body.birthday, gender: req.body.gender});
}
});
});
} else {
res.redirect('/');
res.end();
}

如何修复它?

最佳答案

为了帮助调试,也许您可​​以这样修改代码:

// ... your code before

else {
connection.release();
console.log('FIRST TIME!');


console.log(req.body);


//This method was called, but the page rendered didn't
res.render(
'signup',
{
id : req.body.id,
name : req.body.name,
birthday: req.body.birthday,
gender : req.body.gender
} ,
function(err, html){
if(err) console.log(err);

console.log(html);
//res.send(html);
// trying same but forcing status
res.status(200).send(html);
}
);
}
});
});
} else {
res.redirect('/');
res.end();
}

关于javascript - 在express.js中,res.render()不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521471/

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