我正在尝试为我的react-native应用程序创建登录屏幕,但是当我尝试登录时,出现错误消息“ JSON解析错误:显示了意外的标记“ var””。
我在本地主机访问的另一个文件夹中有一个React Native应用程序和一个服务器应用程序。
我尝试更改var以查看会发生什么,但是随后我将得到相同的错误,但用let代替。
这是我的本机代码
login = () => {
fetch('http://192.168.0.105:8888/loginbackend/routes/users.js', {
method: 'POST',
header: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password,
})
})
.then((response) => response.json())
.then ((res) => {
if (res.success === true) {
AsyncStorage.setItem('user', res.user);
this.props.navigation.navigate('Profile');
}
else {
alert(res.message);
}
})
.done();
}
}
我的服务器文件
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'users'
});
router.POST('/', function(req, res) {
var username = req.body.username;
var password = req.body.password;
connection.query(
"SELECT * FROM user WHERE username = ? AND password = ?",
[username, password], function (err, row){
if (err) {
console.log(err);
res.send({ 'success': false, 'message': 'Kan niet met de database verbinden'});
}
if (row.length > 0 ){
res.send({ 'success': true, 'user': row[0].username });
} else {
res.send({ 'success': false, 'message': 'Gebruiker niet gevonden'});
}
});
});
module.exports = router;
之前,我遇到了JSON Parse错误:意外令牌'<',但是现在它的JSON Parse错误:意外标识符“ var”。如果凭据不正确,我将无法登录,也不会收到警报。
答案 0 :(得分:0)
Express设置不正确,在我的应用程序文件中,我已将“ header”更改为“ headers”。
应用文件
login = () => {
fetch('http://localhost:8080/users/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
"username": this.state.username,
"password": this.state.password,
})
})
.then((response) => response.json())
.then ((res) => {
if (res.success === true) {
AsyncStorage.setItem('user', res.user);
this.props.navigation.navigate('Profile');
}
else {
alert(res.message);
}
})
.done();
}
}
表达文件App.js
const port = 8080;
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.listen(port, () => console.log(`Backend now listening on ${port}!`));
module.exports = app;
表达users.js
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'users',
port: 8889
});
router.post('/', function(req, res) {
var username = req.body.username;
var password = req.body.password;
connection.query(
"SELECT * FROM user WHERE username = ? AND password = ?",
[username, password], function (err, row){
if (err) {
console.log(err);
res.send({ 'success': false, 'message': 'Kan niet met de database verbinden'});
}
if (row.length > 0 ){
res.send({ 'success': true, 'user': row[0].username });
} else {
res.send({ 'success': false, 'message': 'Gebruiker niet gevonden'});
}
});
});
module.exports = router;
希望这对卡在同一事物上的人有所帮助。