我在某些变量方面遇到了一些问题。
我的代码是:
import React from 'react';
import mongoose from 'mongoose';
var sch = require('../models/schema');
export default class IndexPage extends React.Component {
render() {
sch.find(function(err,models){
//???
});
return (
<div className="home">
Afiez ceva {models}
</div>
);
}
}
如何实现这一目标?我已经尝试了所有的可能性。 我希望能够渲染像 {models} 或 {models[0].whatever} 这样的东西。 请给点建议?
引用文献:
import mongoose from 'mongoose';
var schValue = new mongoose.Schema({
asd: String
});
schValue.set( 'collection', 'ecommerce' );
module.exports = mongoose.model('sch', schValue);
最佳答案
我还没有测试过它,但你需要这样的东西吗:
export default class IndexPage extends React.Component {
constructor() {
super();
this.state = {
models: [],
};
}
componentWillMount() {
sch.find({}, (err, models) => {
this.setState({ models });
});
}
render() {
return (
<div className="home">
{this.state.models.map(model => {
return model;
})}
</div>
);
}
}
关于javascript - 在 React 中使用映射函数之外的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42356011/