且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase获取所有用户名&用户ID以用户输入的字符开头

更新时间:2023-01-30 09:29:39

Firebase数据库查询进行前缀匹配,而不是包含.但是,由于仅指定startAt(...),因此查询将匹配名称以前缀开头的所有用户,包括其后的所有名称.如果只需要以前缀字符串开头的结果,则还需要使用endAt(...):

Firebase Database queries do a prefix match, not a contains. But since you only specify startAt(...) the query matches all users from the ones whose name starts with the prefix, including all names after it. If you only want results that start with the prefix string, you'll want to also use endAt(...):

const query = ref.orderByChild('username').startAt(action.searchText)endA‌t(action.searchText+‌​"\uf8ff");
const snapshot = yield call([query, query.once], 'value');
snapshot.forEach(function(child) { 
  console.log(child.key, child.val().username);
});