[英]Firebase Realtime Database, only send data if the key && date don't exists
I have done a lot of research and read through multiple stackoverflow questionso on firebase.我做了很多研究,并阅读了关于 firebase 的多个 stackoverflow 问题。 Some were kind of helpful.
有些很有帮助。 But none that particularly helped my problem.
但没有一个特别有助于我的问题。 I am trying to make a menu planner.
我正在尝试制作菜单规划师。 There for I need more than one check to test if for example.
例如,我需要不止一张支票来测试。 Date exists, serviceType is === to "lunch" ||
日期存在,serviceType 是 === 到“午餐” || "dinner".
“晚餐”。
I have a function that adds and image saves url and input data to a state in react.我有一个功能,可以添加图像并将 url 和输入数据保存到反应状态。 I then use, useEffect to listen for the state update and send the data to firebase.
然后我使用 useEffect 来监听状态更新并将数据发送到 firebase。 I only want to send new data to firebase IF all the checks pass and they don't exists.
如果所有检查都通过并且它们不存在,我只想将新数据发送到 firebase。 Other wise I want to update my data.
否则我想更新我的数据。 I have tried.
我试过了。 Foreach, for in, ref('link/'+ localState.id).
Foreach, for in, ref('link/'+ localState.id)。 Many different methods that I can't seem to get right.
许多不同的方法,我似乎无法正确。
This is the form Submit function:这是表单提交功能:
const handleSubmit = (e) => {
e.preventDefault();
if (image) {
const uploadTask = storage.ref(`images/${image.name}`).put(image);
uploadTask.on(
"state_changed",
(snapshot) => {
const progress = Math.round(
(snapshot.bytesTransferred / snapshot.totalBytes) * 100
);
setProgress(progress);
},
(error) => {
setError(error);
},
() => {
storage
.ref("images")
.child(image.name)
.getDownloadURL()
.then((url) => {
const value = state.serviceType;
const keys = Object.keys(value);
var filtered = keys.filter(function (key) {
return value[key];
});
/////////I have also tried this method, to use one function for all instead of using useEffect//////
const mealData = db().ref().child("meals");
const newMealdata = mealData.push();
// newMealdata.set({
// serviceId: newMealdata.key,
// date: props.dates,
// serviceType: filtered,
// service: {
// mealService: state,
// image: url,
// },
// });
///////////////////////////////////////////////
setNewState((prev) => {
return {
...newState,
date: props.dates,
serviceType: filtered,
serviceId: newMealdata.key,
service: state,
};
});
setProgress(0);
});
}
);
} else {
setError("Error Please Choose an Image to Upload");
}
};
useEffect function useEffect 函数
useEffect(() => {
console.log("newState mounterd: ", newState);
db()
.ref("meals/" + newState.serviceId)
.set({ newState });
}, [newState]);
I have tried using the state.id to check if it equals to the snapshot.key and although they do check.我曾尝试使用 state.id 来检查它是否等于 snapshot.key,尽管他们确实进行了检查。 firebase still creates a new node of data with the same date, but with different Ids and the data I want to update instead.
firebase 仍然创建一个具有相同日期的新数据节点,但具有不同的 Id 和我想要更新的数据。
So how would it be best to say:那么最好怎么说:
if(snapshot.val().date === props.dates && snapshot.val().serviceId === snapshot.key && servicetype === "lunch" || 'dinner'){
update({with new inserted data})
}else if(data doesn't match){
ref('meals').set({createNewNode})
}
The Firebase databases implement fairly limited query support. Firebase 数据库实现了相当有限的查询支持。 To expand that you often have to:
要扩展它,您通常必须:
There is no way to implement your conditions in a query on the Realtime Database, since:无法在实时数据库的查询中实现您的条件,因为:
You can sometimes combine multiple AND conditions into a single query, by adding a synthetic property to your data, such as "dates_serviceId": "..._..."
.您有时可以通过向数据添加综合属性(例如
"dates_serviceId": "..._..."
将多个 AND 条件组合到一个查询中。 With such a property in place, you can then query for ref.orderBy("dates_serviceId").equalTo("myDate_myServiceId")
.有了这样的属性,您就可以查询
ref.orderBy("dates_serviceId").equalTo("myDate_myServiceId")
。
To be able to query on nodes that have either lunch or dinner service, you'd have to expand your data model to explicitly identify such nodes, for example by adding a "lunch_or_dinner": true
property to them.为了能够查询提供午餐或晚餐服务的节点,您必须扩展数据模型以明确标识此类节点,例如通过向它们添加
"lunch_or_dinner": true
属性。 With such a node, you can then query: ref.orderBy("lunch_or_dinner").equalTo(true)
有了这样的节点,你就可以查询:
ref.orderBy("lunch_or_dinner").equalTo(true)
By combining these two approaches, you could even create a single property for the entire query ( dates_serviceId_lunch-or-dinner
), although I must admit that is becoming a bit overly specific even to my taste.通过结合这两种方法,您甚至可以为整个查询创建一个属性 (
dates_serviceId_lunch-or-dinner
),尽管我必须承认,即使我的口味,这也变得有点过于具体了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.