我正在尝试在注册用户时验证验证码。查询有多个条件。如果返回了文档,则验证码将被验证,否则不会被验证。来自以下代码段的问题,尽管未返回文档,但始终满足if(foundUser)。
<template>
<el-select
v-model="selectTag.values"
placeholder="select profiles"
multiple
collapse-tags
filterable
remote
@change="values => handleSelectAll(values, 'routingProfile')"
:filter-method="handleFilter"
>
<el-option
v-for="option in selectTag.options"
:key="option.value"
:label="option.label"
:value="option.value"
>
</el-option>
</el-select>
</template>
<script>
import { Select, Option } from 'element-ui';
export default {
props: {},
components: {
[Select.name]: Select,
[Option.name]: Option
},
data() {
return {
selectTag: {
prvsState: [],
loading: false,
count: 0,
values: [],
options: [
{ value: 'all', label: 'SELECT/DESELECT ALL' },
{ value: 'value1', label: 'label1' },
{ value: 'value2', label: 'label2' }
]
}
};
},
methods: {
handleFilter(query) {
var searchQuery = query.toLowerCase();
var formField = this.selectTag;
var fieldOptions = formField.options;
var filterOptions = [{ value: 'all', label: 'Select/unselect all'
}];
fieldOptions.forEach(option => {
let optionLabel = option.label.toLowerCase();
if (optionLabel.includes(searchQuery)) {
filterOptions.push(option);
}
});
formField.options = filterOptions;
},
handleSelectAll(selectedValues) {
var fieldOptions = this.selectTag.options;
var prvsState = this.selectTag.prvsState;
if (selectedValues.includes('all')) {
if (prvsState.includes('all')) {
this.selectTag.values = [];
this.selectTag.prvsState = [];
} else {
this.selectTag.prvsState = selectedValues;
this.selectTag.values = [];
fieldOptions.forEach(option => {
if (option.value != 'all') {
this.selectTag.values.push(option.value);
}
});
}
} else {
this.selectTag.values = selectedValues;
this.selectTag.prvsState = selectedValues;
}
}
}
};
</script>
答案 0 :(得分:0)
foundUser可以用3种方式编写
if (foundUser.length !== 0) {}
if (!foundUser.length) {}
if (foundUser.length > 0) {}
选择最适合您的一种。
我建议选择2号
UserModel
.find({
email,
verificationCode: code
},
(err, foundUser) => {
if(err) {
return res.json({msg: "Error"})
}
else {
console.log('foundUser', foundUser)
if (!foundUser.length) {
return res.json({msg: "found email + code"})
}
else {
return res.json({msg: "No email & code matched"})
}
}
})