【发布时间】:2021-10-07 09:25:45
【问题描述】:
在 MERN iOS react-native 应用程序中,如何根据登录的用户(即电子邮件)在 Mongodb 地图集中查询正确的 JSON 条目?一个基本特征。举个具体的例子,找出当天登录用户(电子邮件)的阅读量(分钟阅读量),然后在移动应用程序的前端将其发回给他们。以下是相关代码:
在登录时,他们填写以下内容(猫鼬模式):
/* user.model.js */
const userSchema = new Schema(
{
email: {
type: String,
required: true,
unique: true,
trim: true,
},
password: {
type: String,
required: true,
trim: true,
minlength: 6,
},
},
{
timestamps: true,
}
);
const User = mongoose.model("User", userSchema);
现在,登录后在应用程序中,他们可以填写以下条目(猫鼬模式):
/* log.model.js */
const logSchema = new Schema(
{
minutesRead: {
type: number,
required: true,
},
date: {
type: Date,
required: true,
},
},
{
timestamps: true,
}
);
const Log = mongoose.model("Log", logSchema);
这是我记录分钟的路线阅读:
const Log = require("../models/log.model");
router.route("/add").post((req, res) => {
const minutesRead = req.body.minutesRead;
// ..etc
const newLog = new Log({ minutesRead, /* ..etc */ });
newLog.save().then(() => res.json("Log added")).catch(error => //throw error);
}
现在在前端显示登录用户的具体分钟读取数据:
/* Home.js screen */
const url = "http://localhost....";
const home = ({ navigation }) => {
const [totalMinutesRead, setMinutes] = React.useState("");
axios.get(url).then(response => {
setMinutes(() => {
/* sum all of user's minutesRead entries in a loop here */
});
});
return (
<View>
<Text>{totalMinutesRead}</Text>
</View>
)
};
【问题讨论】:
标签: mongodb react-native express mongoose axios