gpt4 book ai didi

javascript - 更改所有唯一键中的某些值

转载 作者:行者123 更新时间:2023-12-03 03:55:45 30 4
gpt4 key购买 nike

所以我正在尝试编写 http 触发函数,该函数将更改每个唯一键(用户 ID)中的特定值。我已经写了一些东西,但输出不是我需要的。它不会更改现有值,而是创建新的用户 ID。

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp(functions.config().firebase)

exports.picksReset = functions.https.onRequest((req, res) => {

const ref = admin.database().ref()

const usersPicked = []

ref.child('users').once('value').then(snap => {
snap.forEach(childSnap => {

ref.child('/users/{userId}').push({picksDone: "0"})
console.log("Changing value...")

})
})

res.send("Done!")

})

我的数据库的图像...

enter image description here

最佳答案

使用.push()创建一个带有pushId的新子项。相反,请使用 .update()。请参阅文档 here 中的详细信息。如果您想要更改每个用户中的子 picksDone,您可以这样做:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp(functions.config().firebase)

exports.picksReset = functions.https.onRequest((req, res) => {

const ref = admin.database().ref()

const usersPicked = []

ref.child('users').once('value').then(snap => {
snap.forEach(childSnap => {
const key = childSnap.key
ref.child(`users/${key}`).update({picksDone: "0"})
console.log("Changing value...")

})
})
res.send("Done!")
})

关于javascript - 更改所有唯一键中的某些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44957898/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com