watch is the observer property in Vue.js, used to monitor data changes and perform corresponding operations.
'form.title' is a listener that listens for changes in the title attribute in the form object.
When the title attribute changes, the callback function will be executed. This function has a parameter oldVal, which represents the value before the change. console.log(oldVal); prints the value of oldVal, which can be used for debugging and verification.
const users = uniCloud.importObject("global"); Import the cloud function object named "global".
const regex = oldVal; Save oldVal directly into the variable regex as a regular expression in string form.
const dbSearchFields = ['title']; Assume that dbSearchFields is the array of fields you want to search, which only contains a single field title.
const queryRe = { $or: dbSearchFields.map(name => ({ [name]: { $regex: .${regex}., $options: 'i' } })) }; Create query object queryRe, use $ The or condition implements fuzzy query on multiple fields.
Among them, the regular expression is .*${regex}.*, which means that the content of the regex can be matched at any position, and $options: 'i' specifies that it is not case-sensitive.
users.getDataList("shop", queryRe, "asc", "title") calls the method named getDataList, queries the database named "shop" according to the filter condition queryRe, and sorts in ascending order by the title field.
.then(res => { console.log(res); }) If the query is successful, print the returned results to the console.
.catch(err => { console.log(err); }) If the query fails, print the error message to the console.
Signed-off-by: 蓝斑 <[email protected]>
watch: {
'form.title'(oldVal) {
console.log(oldVal);
const users = uniCloud.importObject("global");
const regex = oldVal; // 将 oldVal 直接作为字符串形式的正则表达式
const dbSearchFields = ['title']; // 假设这是你要搜索的字段数组
const queryRe = { $or: dbSearchFields.map(name => ({ [name]: { $regex: `.*${regex}.*`, $options: 'i' } })) };
// 在这里执行相关操作
users.getDataList("shop", queryRe, "asc", "title")
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
}
}