最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

错误:Reference.set失败:第一个参数包含属性中未定义的参数

运维笔记admin11浏览0评论

错误:Reference.set失败:第一个参数包含属性中未定义的参数

错误:Reference.set失败:第一个参数包含属性中未定义的参数

我正在尝试在Nodejs / express应用程序中测试POST方法。我已连接到Firebase数据库。我的问题主要与错误有关。我在做什么错,请问该如何解决?

这是错误报告:

PS C:\ Users \ WorkoutApp_v1>节点app.js服务器在以下端口上启动:3000

错误:Reference.set失败:第一个参数包含未定义的属性'workouts。-Lqkqtcf6e2RED2F_1av.name'

这是带有POST方法的execution.js文件。

const express = require('express');
const router = express.Router();
const firebase = require('firebase');


router.get('/add', function(req,res,next) {
    res.render('workouts/add');
});

router.post('/add', function(req,res,next) {
     var workout = {
       name: req.body.name,
       discription: req.body.discription,
       set: req.body.set,
       repsTime: req.body.repsTime
     }
    // console.log(workout);
    // return;

    // const fbRef = firebase.database().ref();
    // var dbRef = fbRef.child('workouts');
    // dbRef.push().set(workout);


    // alternative implementation of the above 3-lines
    firebase.database().ref().child('workouts').push().set(workout);

    req.flash('success_msg', 'Workout saved');
    res.redirect('/workouts');
});

module.exports = router;

这是add.ejs文件。

   <form method="post" action="/workouts/add">
  <div class="form-group">
    <label>Exercise</label>
    <input type="text" class="form-control" name="name" placeholder="Workout Name">
    <label>Description</label>
    <input type="text" class="form-control" name="description" placeholder="Description">
    <label>Set</label>
    <input type="text" class="form-control" name="sets" placeholder="Number of sets">
    <label>RepsTime</label>
    <input type="text" class="form-control" name="repsTime" placeholder="Number of repsTime">
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
   <a class="btn btn-danger" href="/workouts">Close</a>
</form>
回答如下:

无法在RTDB中写入undefined值。

undefined值是访问不存在的对象属性时得到的。

您的req.body.nameundefined,因为req.body没有name属性。

您注释掉的console.log(workout)打印什么?

[编写可能导致在RTDB中编写undefined的代码时,应将其替换为其他内容。在这种情况下,您可以使用req.body.name || ''将丢失的name属性替换为空字符串。

通常,使用||可能会造成麻烦,因为0''之类的值等同于false,因此它们将被您的默认值替换。

更安全的方法是value === undefined ? defaultValue : value

发布评论

评论列表(0)

  1. 暂无评论