admin管理员组文章数量:1391947
I am using firebase and react. I am mapping through the database trying to assign a key to each item. I've done this before and had no problems but for some reason, React doesn't like it. I using ponentDidMount to sync my app to firebase and this is where I'm grabbing each item and the unique key from firebase. Also, console.log gives me the correct key I want.
ponentDidMount() {
this.todosRef.on('child_added', snapshot => {
const todo = snapshot.val();
todo.key = snapshot.key;
console.log(todo.key);
this.setState({ todos: this.state.todos.concat( todo ) });
});
}
but when I map through the array and assign the key it throws the error : Cannot create property 'key' on string '
Here is how I am mapping:
const listOfTodos = this.state.todos.map( (todo) => {
return <li key={todo.key}>{todo}</li>
});
I am using firebase and react. I am mapping through the database trying to assign a key to each item. I've done this before and had no problems but for some reason, React doesn't like it. I using ponentDidMount to sync my app to firebase and this is where I'm grabbing each item and the unique key from firebase. Also, console.log gives me the correct key I want.
ponentDidMount() {
this.todosRef.on('child_added', snapshot => {
const todo = snapshot.val();
todo.key = snapshot.key;
console.log(todo.key);
this.setState({ todos: this.state.todos.concat( todo ) });
});
}
but when I map through the array and assign the key it throws the error : Cannot create property 'key' on string '
Here is how I am mapping:
const listOfTodos = this.state.todos.map( (todo) => {
return <li key={todo.key}>{todo}</li>
});
Share
Improve this question
edited Jun 30, 2018 at 13:46
Frank van Puffelen
601k85 gold badges890 silver badges860 bronze badges
asked Jun 29, 2018 at 18:00
Devon BurleyDevon Burley
651 silver badge6 bronze badges
4
-
What's the value of
snapshot.val()
? I would suspect it is a string, and trying to set a key on that string withtodo.key = snapshot.key
causes your error. – Tholle Commented Jun 29, 2018 at 18:02 -
Not being entirely sure what
todo
is, why not create a new object containingtodo
and yourkey
? – Chris Commented Jun 29, 2018 at 18:03 -
I have one suggestion. Write like this ` let todo = []; todo['val'] = snapshot.val(); todo['key'] = snapshot.key;` and then use
const listOfTodos = this.state.todos.map( (todo) => { return <li key={todo.key}>{todo.val}</li> });
– Aram Grigoryan Commented Jun 29, 2018 at 18:07 -
@Tholle
snapshot.val()
returns each item from the firebase database as a string. – Devon Burley Commented Jun 29, 2018 at 18:17
1 Answer
Reset to default 5I believe what you have is an immutable object which you're trying to modify.
https://firebase.google./docs/reference/js/firebase.database.DataSnapshot
A DataSnapshot is an efficiently generated, immutable copy of the data at a Database location. It cannot be modified and will never change (to modify data, you always call the set() method on a Reference directly).
Try putting your todo and key into a new object like this:
const todo = { key: snapshot.key, value: snapshot.val() }
本文标签: javascriptCannot create property 39key39 on stringStack Overflow
版权声明:本文标题:javascript - Cannot create property 'key' on string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744679133a2619292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论