未捕获错误:Reference.push失败:第一个参数在属性中包含NaN

时间:2018-04-24 18:21:44

标签: reactjs firebase firebase-realtime-database react-redux

尝试向firebase数据库添加费用时收到此错误:

Uncaught Error: Reference.push failed: first argument contains NaN in property 'users.mzsNqPucvWZMWuyo4sThlMqyR1k2.expenses.km'

Validation.js react-dom.development.js

获取firebase警告。

FIREBASE WARNING: set at /users/mzsNqPucvWZMWuyo4sThlMqyR1k2/expenses/-LAsp1j0HVUP-1Y32UMd failed: permission_denied

然后,即使我在firebase规则中将.read和.write规则设置为true,承诺也会删除权限。

Uncaught (in promise) Error: PERMISSION_DENIED: Permission denied
    at Repo.js:527

可能有用的代码段。 STill不知道如何摆脱被拒绝的许可。

Firebase规则供参考:

"rules": {
    ".read": true,
    ".write": true,
      "users": {
        "$user_id": {
          ".read": "$user_id === auth.uid",
          ".write": "$user_id === auth.uid",
            "expenses": {
              "$expense_id": {
                ".validate": "newData.hasChildren(['title', 'amount', 'details', 'createdAt', 'category', 'km'])",
                  "title": {
                    ".validate": "newData.isString() && newData.val().length > 0"
                  },
                  "amount": {
                    ".validate": "newData.isNumber()"
                  },

费用添加操作(redux)

const {
            title = '',
            details = '',
            amount = 0,
            createdAt = 0,
            category = '',
            km = 0
        } = expenseData;
        const expense = { title, details, amount, createdAt, category, km };
        database.ref(`users/${uid}/expenses`).push(expense).then((ref) => {
            dispatch(addExpense({
                id: ref.key,
                ...expense
            }));
        });

我提交表单时收到此错误,因此我会将其包含在内。

添加Exp

export class AddExp extends React.Component {
    onSubmit = (expense) => {
        this.props.startAddExp(expense);
        this.props.history.push('/');
    };
    render() {
        return (
        <div>
            <div>
                <ExpenseForm 
                path={this.props.location.pathname}
                onSubmit={this.onSubmit}/>
            </div>
        </div>
        )
    }
}

费用表单onSubmit()

onSubmit = (e) => {
    e.preventDefault();
    if (!this.state.title || !this.state.amount) {
        this.setState(() => ({ error: 'Error'}))
    } else {
        this.setState(() => ({ error: 'error' }));
        this.props.onSubmit({
            title: this.state.title,
            amount: parseFloat(this.state.amount, 10) * 100,
            createdAt: this.state.createdAt.valueOf(),
            details: this.state.details,
            category: this.state.category,
            km: parseFloat(this.state.km, 10) * 100
        });
    }
   };

我不认为它与代码有关,似乎更像firebase正在阻止插入。

2 个答案:

答案 0 :(得分:1)

如果验证规则失败,他们仍会发回PERMISSION DENIED错误。我会仔细检查您的验证规则,以确保它们涵盖边缘情况,例如title''时(newData.val().length 0> 0而非FieldValue.serverTimestamp()失败) )。您已通过删除验证规则(which don't cascade)在答案中对此进行了测试。随意发布您的其他规则以及失败的原因及其原因。

答案 1 :(得分:0)

当我将firebase中的规则更改为只读&amp;时,错误就消失了。写。

{
  "rules": {
    ".read": true,
    ".write": true,
  }
}

但是我仍然不明白我以前的规则集有什么问题,但现在这样做了。