pouchdb/react,如果数据库中没有结果,则将状态值设置为零
pouchdb/react, set state value to zero if no result is in the database
我有一段代码,只要数据库中有东西就可以完美运行,但这会导致应用程序在第一次加载时崩溃,因为在新部署时数据库中没有结果集。现在,如果我设置数据并加载此屏幕,它会从 pouchdb 获取结果集并获取文档,然后根据文档索引设置状态值。
当前在新负载上,我收到错误消息,它无法读取 newBaselineDocs[0]
的未定义的 属性
我需要对此进行更改,以便如果数据库中没有结果,那么我会将这些值的状态全面设置为零。
例如,如果没有结果,那么我会使用 ldl: 0
而不是 ldl: newBaselineDocs[0].ldl
,如果这有意义的话。我知道它会像 if/else 但我不知道如何使用 pouchdb 结果逻辑来实现它。
setBaselineMax = () => {
this.state.baselineDB.db.find({
selector: {
$and: [
{_id: {"$gte": null}},
{ldl: {$exists:true}},
{hdl: {$exists:true}},
{totalChol: {$exists:true}},
{weight: {$exists:true}},
{water: {$exists:true}},
{sleep: {$exists:true}},
{activity: {$exists:true}},
{heartRate: {$exists:true}},
{netCalories: {$exists:true}},
{bloodPresure: {$exists:true}},
{whiteCount: {$exists:true}},
{redCount: {$exists:true}},
{trig: {$exists:true}},
{glucose: {$exists:true}},
{oxygen: {$exists:true}},
{psa: {$exists:true}},
{alcohol: {$exists:true}},
{createdAt: {$exists: true}}
]
},
fields: ['_id','ldl','hdl','totalChol','weight','water','sleep','activity','heartRate','netCalories','bloodPresure','whiteCount','redCount','trig','glucose','oxygen','psa', 'alcohol'],
sort: [{'_id':'asc'}],
limit: 1
}).then(result => {
console.log('baseline result');
console.log(result);
const newBaselineDocs = result.docs;
console.log('this is basline data from app');
console.log(newBaselineDocs);
this.setState(prevState => ({
baselineRecord: { // object that we want to update
...prevState.baselineRecord, // keep all other key-value pairs
ldl: newBaselineDocs[0].ldl,
hdl: newBaselineDocs[0].hdl,
totalChol: newBaselineDocs[0].totalChol,
weight: newBaselineDocs[0].weight,
water: newBaselineDocs[0].water,
sleep: newBaselineDocs[0].sleep,
activity: newBaselineDocs[0].activity,
heartRate: newBaselineDocs[0].heartRate,
netCalories: newBaselineDocs[0].netCalories,
bloodPresure: newBaselineDocs[0].bloodPresure,
whiteCount: newBaselineDocs[0].whiteCount,
redCount: newBaselineDocs[0].redCount,
trig: newBaselineDocs[0].trig,
glucose: newBaselineDocs[0].glucose,
oxygen: newBaselineDocs[0].oxygen,
psa: newBaselineDocs[0].psa,
alcohol: newBaselineDocs[0].alcohol // update the value of specific key
}
}))
这应该在评论中,但这会是一团糟。该错误是预期的,因为 result.docs
为空,而您的代码假定它永远不会为空。
因为 newBaselineDocs
是空的,因为 result.docs
是空的,所以 newBaselineDocs[0][someProperty]
是未定义的。
这很容易修复,只需检查 result.docs 是否为空,如果是,则使用您的 'zero across the board' 方法进行初始化,例如
// snip
}).then(result => {
let doc;
if(result.docs.length === 0) {
// initialize doc with default values
doc = {
// default property values
};
}
// do something with doc
});
我有一段代码,只要数据库中有东西就可以完美运行,但这会导致应用程序在第一次加载时崩溃,因为在新部署时数据库中没有结果集。现在,如果我设置数据并加载此屏幕,它会从 pouchdb 获取结果集并获取文档,然后根据文档索引设置状态值。
当前在新负载上,我收到错误消息,它无法读取 newBaselineDocs[0]
我需要对此进行更改,以便如果数据库中没有结果,那么我会将这些值的状态全面设置为零。
例如,如果没有结果,那么我会使用 ldl: 0
而不是 ldl: newBaselineDocs[0].ldl
,如果这有意义的话。我知道它会像 if/else 但我不知道如何使用 pouchdb 结果逻辑来实现它。
setBaselineMax = () => {
this.state.baselineDB.db.find({
selector: {
$and: [
{_id: {"$gte": null}},
{ldl: {$exists:true}},
{hdl: {$exists:true}},
{totalChol: {$exists:true}},
{weight: {$exists:true}},
{water: {$exists:true}},
{sleep: {$exists:true}},
{activity: {$exists:true}},
{heartRate: {$exists:true}},
{netCalories: {$exists:true}},
{bloodPresure: {$exists:true}},
{whiteCount: {$exists:true}},
{redCount: {$exists:true}},
{trig: {$exists:true}},
{glucose: {$exists:true}},
{oxygen: {$exists:true}},
{psa: {$exists:true}},
{alcohol: {$exists:true}},
{createdAt: {$exists: true}}
]
},
fields: ['_id','ldl','hdl','totalChol','weight','water','sleep','activity','heartRate','netCalories','bloodPresure','whiteCount','redCount','trig','glucose','oxygen','psa', 'alcohol'],
sort: [{'_id':'asc'}],
limit: 1
}).then(result => {
console.log('baseline result');
console.log(result);
const newBaselineDocs = result.docs;
console.log('this is basline data from app');
console.log(newBaselineDocs);
this.setState(prevState => ({
baselineRecord: { // object that we want to update
...prevState.baselineRecord, // keep all other key-value pairs
ldl: newBaselineDocs[0].ldl,
hdl: newBaselineDocs[0].hdl,
totalChol: newBaselineDocs[0].totalChol,
weight: newBaselineDocs[0].weight,
water: newBaselineDocs[0].water,
sleep: newBaselineDocs[0].sleep,
activity: newBaselineDocs[0].activity,
heartRate: newBaselineDocs[0].heartRate,
netCalories: newBaselineDocs[0].netCalories,
bloodPresure: newBaselineDocs[0].bloodPresure,
whiteCount: newBaselineDocs[0].whiteCount,
redCount: newBaselineDocs[0].redCount,
trig: newBaselineDocs[0].trig,
glucose: newBaselineDocs[0].glucose,
oxygen: newBaselineDocs[0].oxygen,
psa: newBaselineDocs[0].psa,
alcohol: newBaselineDocs[0].alcohol // update the value of specific key
}
}))
这应该在评论中,但这会是一团糟。该错误是预期的,因为 result.docs
为空,而您的代码假定它永远不会为空。
因为 newBaselineDocs
是空的,因为 result.docs
是空的,所以 newBaselineDocs[0][someProperty]
是未定义的。
这很容易修复,只需检查 result.docs 是否为空,如果是,则使用您的 'zero across the board' 方法进行初始化,例如
// snip
}).then(result => {
let doc;
if(result.docs.length === 0) {
// initialize doc with default values
doc = {
// default property values
};
}
// do something with doc
});