Firebase JS SDK中的文档有点晦涩难懂,但是从我的收集来看,如果我想更新列表的最后一项,我必须像这样引用列表并进行链式更新吗?
const { uid } = await firebase.auth().currentUser;
const ref = firebase
.app()
.database()
.ref();
const userRef = ref
.child('users/' + uid + '/myList')
.orderByKey()
.limitToLast(1)
.update({ stamp: Date.now() });
但是没有骰子。
// TypeError: ref.child(...).orderByKey(...).limitToLast(...).update is not a function
更新:
我可以清楚地看到我的引用的toString值正确,但是在短时间内不存在该值吗?
const { uid } = await firebase.auth().currentUser;
const ref = firebase
.database()
.ref('users/' + uid)
.child('myList');
console.log(ref.toString()); // firebaseio.com/users/XtNjKhhF...lfBn2/myList
ref.on('value', function(snap) {
console.log('snap val', snap.val()); // never called
});
// data is clearly there, no?
{
"users" : {
"Xt....lfABn2" : {
"myList" : {
"-LZDd5G2lhNv5ogv" : {
"start" : 1550728848450
}
}
}
}
}
答案 0 :(得分:0)
此代码返回一个快照,但是您将其视为参考
const userRef = ref
.child('users/' + uid + '/myList')
.orderByKey()
.limitToLast(1)
您无法在快照上执行update()
.update({ stamp: Date.now() });
这就是为什么您需要在快照中添加.ref
const userRef = ref
.child('users/' + uid + '/myList')
.orderByKey()
.limitToLast(1)
.ref // <- this line here
.update({ stamp: Date.now() });