我想在javascript对象中获得一个值,但它以某种方式失败。我设法通过findOne方法从mongoDB获取预期数据。这是我的代码和控制台日志。如何将对象从容器传递给Meteor和React中的类?
const title = Questions.findOne({_id: props.match.params.id});
console.log(title);
然后控制台说:
对象{_id: “bpMgRnZxh5L4rQjP9”,文: “你喜欢苹果吗?”}
我想要得到什么只在对象中的文本。我已经尝试过这些。
console.log(title.text);
console.log(title[text]);
console.log(title["text"]);
console.log(title[0].text);
但我无法访问它...错误消息如下。
遗漏的类型错误:无法读取的不确定
这听起来超级简单财产“文”,但我不能由我自己解决。任何人都可以帮我吗?
其他上下文 我正在使用Meteor和React。我想将对象内部的文本从容器传递给类。我想渲染render()中的文本。但它不接收来自容器的任何数据...容器中的console.log运行良好并显示该对象。
import React, { Component } from 'react';
import { createContainer } from 'meteor/react-meteor-data';
import { Questions } from '../../api/questions.js';
import PropTypes from 'prop-types';
import { Answers } from '../../api/answers.js';
import ReactDOM from 'react-dom';
import { Chart } from 'react-google-charts';
class MapClass extends React.Component{
handleAlternate(event){
event.preventDefault();
const country = ReactDOM.findDOMNode(this.refs.textInput).value.trim();
Answers.insert({
country,
yes: false,
question_id:this.props.match._id,
createdAt: new Date(), // current time
});
ReactDOM.findDOMNode(this.refs.textInput).value = '';
}
handleSubmit(event) {
event.preventDefault();
const country = ReactDOM.findDOMNode(this.refs.textInput).value.trim();
Answers.insert({
country,
yes: true,
question_id: this.props.match.params.id,
createdAt: new Date(), // current time
});
ReactDOM.findDOMNode(this.refs.textInput).value = '';
}
constructor(props) {
super(props);
this.state = {
options: {
title: 'Age vs. Weight comparison',
},
data: [
['Country', 'Popularity'],
['South America', 12],
['Canada', 5.5],
['France', 14],
['Russia', 5],
['Australia', 3.5],
],
};
this.state.data.push(['China', 40]);
}
render() {
return (
<div>
<h1>{this.props.title.text}</h1>
<Chart
chartType="GeoChart"
data={this.state.data}
options={this.state.options}
graph_id="ScatterChart"
width="900px"
height="400px"
legend_toggle
/>
<form className="new-task" onSubmit={this.handleSubmit.bind(this)} >
<input
type="text"
ref="textInput"
placeholder="Type to add new tasks"
/>
<button type="submit">Yes</button>
<button onClick={this.handleAlternate.bind(this)}>No</button>
</form>
</div>
);
}
}
export default MapContainer = createContainer(props => {
console.log(Questions.findOne({_id: props.match.params.id}));
return {
title: Questions.findOne({_id: props.match.params.id})
};
}, MapClass);
'findOne'是异步的。请参阅文档中有关如何正确执行此操作的示例。 – str
@str不在[mongo](https://docs.mongodb.com/manual/mongo/)shell中,它不是,所有方法都是**同步**。 '_id'的值表明流星正在使用中。所以如果提问者能够真正确认并且给出他们实际运行陈述的上下文,那将是很好的。 –
@NeilLunn但是示例代码并未在mongo shell中执行。 – str