[英]Cloud Function updating user profile, but prevents users having same username
I currently am working on an edit profile cloud function.我目前正在编辑配置文件云 function。 I have a where clause, to see if a username in my users collection is the same as the req.body.username and if that is the case block the request.我有一个 where 子句,查看我的 users 集合中的用户名是否与 req.body.username 相同,如果是,则阻止请求。 The issue is;问题是; however, I am getting an error that looks like this: "> Error: Value for argument "value" is not a valid query constraint. "undefined" values are only ignored in object properties.但是,我收到如下所示的错误:> 错误:参数“值”的值不是有效的查询约束。“未定义”值仅在 object 属性中被忽略。
at Object.validateUserInput (/Users/robterrell/Desktop/safespace/functions/node_modules/@google-cloud/firestore/build/src/serializer.js:265:19) at validateQueryValue (/Users/robterrell/Desktop/safespace/functions/node_modules/@google-cloud/firestore/build/src/reference.js:2135:18) at CollectionReference.where (/Users/robterrell/Desktop/safespace/functions/node_modules/@google-cloud/firestore/build/src/reference.js:1033:9) at exports.profileUpdate (/Users/robterrell/Desktop/safespace/functions/api/users.js:216:6) at Layer.handle [as handle_request] (/Users/robterrell/Desktop/safespace/functions/node_modules/express/lib/router/layer.js:95:5) at next (/Users/robterrell/Desktop/safespace/functions/node_modules/express/lib/router/route.js:137:13) at /Users/robterrell/Desktop/safespace/functions/util/auth.js:30:14 at processTicksAndRejections (internal/process/task_queues.js:85:5)"
The where clauses doesn't seem to be picking up if I am sending a request with a username that is already being used in my user collection as well.如果我发送的请求中的用户名也已在我的用户集合中使用,那么 where 子句似乎没有出现。 I am unsure of what I am currently doing wrong.我不确定我目前做错了什么。 I am invoking this function by sending a post request via postman.我通过 postman 发送一个发布请求来调用这个 function。 My function looks like this:我的 function 看起来像这样:
exports.profileUpdate = (req, res) => {
if (req.user.email) {
admin.auth().updateUser(req.user.uid, { email: req.body.email })
}
let document = db
.collection('users')
.where('username', '==', req.body.username)
document
.get()
.then(snapshot => {
if (snapshot.empty) {
snapshot.forEach(doc => {
const data = doc.id
db.collection('users').doc(data).update(req.body)
})
return res.json({ message: 'Updated Successfully' })
} else {
return res
.status(400)
.json({ username: 'this username is already taken' })
}
})
.catch(error => {
console.error(error)
return res.status(400).json({
message: 'Cannot Update the value'
})
})
}
I am passing formData(detailsToUpdate) to my function that is triggered by hitting an endpoint with express.我将 formData(detailsToUpdate) 传递给我的 function,这是通过使用 express 命中端点来触发的。 The function looks like this: function 看起来像这样:
import React, { useState } from 'react'
import { Text, Input, Icon } from '@ui-kitten/components'
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
import { View, StyleSheet, TouchableOpacity, Image } from 'react-native'
import AsyncStorage from '@react-native-community/async-storage'
import axios from 'axios'
const updateUsername = ({ navigation, route }) => {
const [errorMessage, setErrorMessage] = useState('')
const { user } = route.params
const [detailsToUpdate, setDetailsToUpdate] = useState({
username: user.username,
userId: user.userId
})
const handleProfileUpdate = async () => {
try {
const token = await AsyncStorage.getItem('FBIdToken')
console.log('im running')
const response = await axios({
method: 'POST',
url:
'http://localhost:5000/safe-space-dev/us-central1/api/users/update',
headers: { Authorization: `Bearer ${token}` },
data: detailsToUpdate
})
// merges the current user data in storage w/ updated details.
await AsyncStorage.mergeItem(
'userDetails',
JSON.stringify(detailsToUpdate)
)
alert(response.data.message)
} catch (error) {
setErrorMessage(error)
}
}
return (
<KeyboardAwareScrollView
extraScrollHeight={20}
enableOnAndroid={true}
keyboardShouldPersistTaps='handled'
style={{ backgroundColor: '#fff' }}
>
<View>
<View
style={{
paddingRight: 20,
paddingLeft: 20
}}
>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 20
}}
>
<TouchableOpacity
onPress={() => {
/* 1. Navigate to the Details route with params */
navigation.navigate('EditProfile')
}}
>
<Icon
style={{ height: 28, width: 28 }}
name='chevron-left'
fill='black'
/>
</TouchableOpacity>
<TouchableOpacity onPress={handleProfileUpdate}>
<Text>Done</Text>
</TouchableOpacity>
</View>
<View
style={{
marginTop: 20,
paddingRight: 20,
paddingLeft: 20
}}
></View>
<View
style={{
height: 75
}}
>
<Text category='p1' appearance='hint' style={{ fontSize: 12 }}>
Username
</Text>
<Input
value={detailsToUpdate.username}
autoCapitalize='none'
autoComplete={false}
onChangeText={value =>
setDetailsToUpdate({ ...detailsToUpdate, username: value })
}
/>
</View>
</View>
</View>
</KeyboardAwareScrollView>
)
}
export default updateUsername
and then here is the middleware for the for the express route:然后是快速路由的中间件:
const { admin, db } = require('./admin')
module.exports = (request, response, next) => {
let idToken
if (
request.headers.authorization &&
request.headers.authorization.startsWith('Bearer ')
) {
idToken = request.headers.authorization.split('Bearer ')[1]
} else {
console.error('No token found')
return response.status(403).json({ error: 'Unauthorized' })
}
admin
.auth()
.verifyIdToken(idToken)
.then(decodedToken => {
request.user = decodedToken
return db
.collection('users')
.where('userId', '==', request.user.uid)
.limit(1)
.get()
})
.then(data => {
request.user.username = data.docs[0].data().username
request.user.imageUrl = data.docs[0].data().imageUrl
request.user.dob = data.docs[0].data().dob
request.user.userId = data.docs[0].data().userId
return next()
})
.catch(err => {
console.error('Error while verifying token', err)
return response.status(403).json(err)
})
}
Based on the error message, the req.body.username
is undefined
.根据错误消息, req.body.username
是undefined
。 Verify you're sending the correct body in the request or that it's not getting removed in other middleware.验证您在请求中发送了正确的正文,或者它没有在其他中间件中被删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.