Gettin MEAN我正在制作自己的申请表。我尝试连接前端到Express中的后端。我复制了文本,但我的console.log仍然给我留言'未找到用户ID'。我想我已经接近完成了。任何提示或有用的链接都表示赞赏。
const userSchema = new Schema({
firstName: String,
lastName: String,
userName: String,
telephone: String,
password: String
});
中的路线
router
.route('/users/:userid')
.get(ctrlUsers.userReadOne);
module.exports = router;
在Postman中,这个get.request有效(例如)
http://localhost:3000/api/users/5ad87da47bb05b0594fff5b6
路线:
const express = require('express');
const router = express.Router();
const ctrlUsers = require('../controllers/users');
router.get('/users/:userid', ctrlUsers.userInfo);
module.exports = router;
const request = require('request');
const apiOptions = {
server: 'http://localhost:3000'
};
if (process.env.NODE_ENV === 'production') {
apiOptions.server = 'https://pure-temple-67771.herokuapp.com';
}
/*Get myprofile */
const userInfo = function (req, res) {
const path = '/api/users/${req.params.userid}';
requestOptions = {
url: apiOptions.server + path,
method: 'GET',
json: {}
};
console.log('path ' + requestOptions.url);
request(
requestOptions,
(err, response, body) => {
_rendermyProfile(req, res, body);
console.log(body); // no userid found
});
};
const _rendermyProfile = function (req, res, userDetail) {
res.render('myProfile', {
profile: {
title: 'Mijn profiel',
firstName: userDetail.firstName,
lastName: userDetail.lastName,
email: userDetail.email,
telephone: userDetail.telephone
}
});
};
module.exports = {
userInfo
};
答案 0 :(得分:0)
当然,您将找不到用户标识,因为您有字符串文字的错误。在你的控制器里面,改变了这个:
const path = '/api/users/${req.params.userid}';
到此:
const path = `/api/users/${req.params.userid}`;
字符串文字仅在使用反斜杠`时才有效。
现在您的应用应该正确提出请求。