javascript - 从数据库读取值并保存在变量中
问题描述
我正在尝试解决此问题,但我不明白该怎么做。
基本上我有一个数据库,其中有用户的信息。这些信息可以通过表格进行修改。当用户想要修改这些值时,会使用用户的 FiscalCode 在数据库中进行搜索。当找到用户时,我想将旧数据保存在一个变量中,因为如果我将一个字段留空(或者我不更改数据),则旧数据已被删除。这样我就使用了旧数据。
所以问题是,如果用户刚刚注册,他除了名字、姓氏和电子邮件之外没有其他数据,这样其他字段都是空的。
问题是当我去读取这些数据以保存在变量中时,如果它们是空的,我就错了,因此改变是不可能的。(错误在readUtente函数中)
我试过这种方法添加一个“如果”,但它不起作用。
componentDidMount(){
this.readUtente(this.props.cf)
console.log("this.props.cf: " + this.props.cf)
}
//Funzione di prova aggiunta
readUtente(cf){
let params = {};
params = {
// "Person.FiscalCode": this.props.cf
"Person.FiscalCode": cf
};
console.log("1. PARAMS IN MODIFICA PROFILO: " +JSON.stringify(params))
global.utente.db.localdb().find({
selector: params
})
.then(response => {
let utente = response.docs[0];
console.log("2. UTENTE: " + JSON.stringify(utente))
console.log("3. Utente.Person.FirstName: " + utente.Person.FirstName)
console.log("4. utente.Person.City: " + utente.Person.City)
//ho aggiunto if
if(utente.Person.FirstName !== undefined) {this.setState({ nome: utente.Person.FirstName })}
if(utente.Person.LastName !== undefined) {this.setState({ cognome: utente.Person.LastName })}
if(utente.Person.City !== undefined) {this.setState({ citta: utente.Person.City })}
if(utente.Person.Address !== undefined) {this.setState({ indirizzo: utente.Person.Address })}
if(utente.Person.DateOfBirth !== undefined) {this.setState({ dataNascita: utente.Person.DateOfBirth })}
if(utente.Person.Country !== undefined) {this.setState({ paese: utente.Person.Country })}
if(utente.Contacts.Email !== undefined) {this.setState({ email: utente.Contacts.Email })}
})
.catch(function(err) {
console.log(JSON.stringify(err));
})
}
findUtente(cf) {
console.log("5. Codice fiscale Passato: " + cf)
let params = {};
params = {
"Person.FiscalCode": cf
};
console.log("6. PARAMS della findUtente" + JSON.stringify(params))
global.utente.db
.localdb()
.find({
selector: params
})
.then(response => {
let utente = response.docs[0];
console.log("7. Utente: " + JSON.stringify(utente))
console.log("8. this.state.City: " + this.state.citta)
utente.Person.FirstName = this.state.FirstName == null ? this.state.nome : this.state.FirstName;
console.log("9. utente.Person.FirstName: " + utente.Person.FirstName)
utente.Person.LastName = this.state.LastName == null ? this.state.cognome : this.state.LastName;
console.log("10. utente.Person.LastName: " + utente.Person.LastName)
utente.Person.City = this.state.City == null ? this.state.citta : this.state.City;
console.log("11. utente.Person.City: " + utente.Person.City)
utente.Person.Address = this.state.Address == null ? this.state.indirizzo : this.state.Address;
utente.Person.DateOfBirth = this.state.DateOfBirth == null ? this.state.dataNascita : this.state.DateOfBirth;
utente.Person.Country = this.state.Country == null ? this.state.paese : this.state.Country;
utente.Contacts.Email = this.state.Email == null ? this.state.email : this.state.Email;
return global.utente.db.localdb().put(utente);
})
.catch(function(err) {
console.log(JSON.stringify(err));
})
.finally(function() {
Actions.homepageutente();
});
}
安慰:
1. PARAMS IN MODIFICA PROFILO: {"Person.FiscalCode":"ZMBNNZ91P16F364F"}
ModificaProfilo.js:34 this.props.cf: ZMBNNZ91P16F364F
ModificaProfilo.js:51 2. UTENTE: {"DocumentType":"GeneralData","Person":{"FirstName":"Nunzio","LastName":"Zambrotti","FiscalCode":"ZMBNNZ91P16F364F"}
ModificaProfilo.js:52 3. Utente.Person.FirstName: Nunzio
ModificaProfilo.js:53 4. utente.Person.City: undefined
ModificaProfilo.js:67 {}
ModificaProfilo.js:72 5. Codice fiscale Passato: ZMBNNZ91P16F364F
ModificaProfilo.js:78 6. PARAMS della findUtente{"Person.FiscalCode":"ZMBNNZ91P16F364F"}
ModificaProfilo.js:86 7. Utente: {"DocumentType":"GeneralData","Person":{"FirstName":"Nunzio","LastName":"Zambrotti","FiscalCode":"ZMBNNZ91P16F364F"}
ModificaProfilo.js:87 8. this.state.City: undefined
ModificaProfilo.js:89 9. utente.Person.FirstName: Nunzio
ModificaProfilo.js:91 10. utente.Person.LastName: Zambrotti
11. utente.Person.City: Rome
解决方案
问题可能是空数据没有被评估为 null,但可能是空字符串或未定义。
尝试通过以下方式更改 if 语句:
if(utente.Contacts.Email !== undefined )
或者
if(utente.Contacts.Email !== "" )
或者
if(utente.Contacts.Email)
推荐阅读
- c++ - 有什么方法可以对不同类型的变量进行相同的计算,只需一个函数?
- node.js - 用猫鼬和nodejs保存另一个文档的参考
- scala - 为什么在scala中展平和collect_list错误?无法解析符号
- javascript - 如何在javascript中触发mousedown vs click事件?
- react-native - 在本机反应中每 3 秒刷新一个函数
- m4 - 包含文件并缩进每一行
- c# - 在使用 WebRTC 进行直播时在不同的相机分辨率之间切换
- git - 为什么在使用 GitFlow 时需要将发布分支合并到开发分支中?
- python - 将 JSON 读入 Python / Pandas - 获取 JSONDecodeError
- go - 如何在golang中使用mux和gorm发布body raw?