I'm working on a Sails JS application and i try to use React in it. I'm new to both React and Sails since a few days. I tried it with a "simple" example, i have a list of user, and i want to display a list of them using React. I have a method "getUsers" in "MainController" that return the users object. When i go to "main/getUsers" on the browser i get :
{
"users": [
{
"id": 6,
"username": "User1",
"password": "sha1$e89ea270$1$5937f3d94f90280e3dc5a435bd8c8128f9d29ab2",
"mail": "test@mail.fr",
"pics": null,
"createdAt": "2016-02-29T12:58:46.000Z",
"updatedAt": "2016-02-29T12:58:46.000Z"
},
{
"id": 3,
"username": "test",
"password": "sha1$88a41992$1$f43d52da22e2082a06625063e36c4a965451094f",
"mail": "test@hotmail.fr",
"pics": null,
"createdAt": "2016-02-29T12:23:57.000Z",
"updatedAt": "2016-02-29T13:05:42.000Z"
},
(...)
]
}
my code to display them is the following :
<div id="example">test</div>
<script type="text/babel">
var User = React.createClass({
render: function () {
<p class='oneUserList col-xs-3 col-md-1'>
<img src="{this.props.pics} />
{this.props.username}
</p>
}
});
var UserList = React.createClass({
getInitialState: function () {
return {users: []};
},
componentDidMount: function () {
$.ajax({
url: "/main/getUsers/",
dataType: 'json',
cache: false
}).exec(function (response) {
this.setState({users: response.records });
}.bind(this));
},
render: function () {
var self = this;
var users = this.state.users.map(function (p) {
return <User username={p.username} pics = {p.pics} />
});
if (!users.length) {
users = <p>Loading users ..</p>;
}
return (
{users}
);
}
});
ReactDOM.render(
<UserList />,
document.getElementById("example")
);
</script>
I tried it many ways but i always have the same error : "SyntaxError: embedded: Unexpected token (x:y)" right now it's on the space before 'url: "/main/getUsers"' but if was on the a of ajax, then on the r of url ..etc and i really don't understand why
If somebody could help me with this, thanks
Aucun commentaire:
Enregistrer un commentaire