【问题标题】:Why do I get this undefined in property error when trying to save to database in Firebase?尝试保存到 Firebase 中的数据库时,为什么会出现此未定义的属性错误?
【发布时间】:2016-05-17 17:34:03
【问题描述】:

我已经编写了允许用户将图像上传到 firebase 数据库的代码。该代码基本上引用了节点'images,然后将图像设置为子节点url。但是我收到以下消息。

Firebase.set failed: First argument contains undefined in property 'images.url'

关于为什么会发生这种情况的任何线索?我的代码如下。

var theBigWeddingBook = angular.module('theBigWeddingBook');

theBigWeddingBook.controller('WeddingBookCtrl', function($scope, $firebase, Upload) {

    var ref= new Firebase('https://the-big-wedding-book.firebaseio.com/');
    var images = $scope.file;

    $scope.submit = function() {
        ref.child('images').set({url : images}).then(function(url) {
            console.log('Image Uploaded!');
        }).catch(function(error) {
            console.log(error);
        })
    }
});

【问题讨论】:

  • 您的图像变量的价值是多少?
  • 值为通过ng文件上传表单加载的图片...所以是'ng-model="images"'
  • Firebase 不支持图片,但如果您将图片转换为 base64 字符串,则可以保存它们。
  • 谢谢,我明天试试

标签: angularjs firebase angularfire firebase-realtime-database


【解决方案1】:

我现在已经重构了我的代码,这阻止了我收到错误。

theBigWeddingBook.controller('WeddingBookCtrl', function($scope, $firebaseArray, $firebaseAuth, $location, Upload) {

    var ref= new Firebase('https://the-big-wedding-book.firebaseio.com/images');
    var auth = $firebaseAuth(ref);

    ref.onAuth(function(authData) {
      if (authData) {
        console.log("Authenticated with uid:", authData.uid, authData);
        $location.path("/your-wedding-book");
      } else {
        console.log("Client unauthenticated.")
      }
    });

    $scope.post = $firebaseArray(ref);

    $scope.submit = function() {
        Upload.base64DataUrl($scope.image).then(function(base64Urls) {
                $scope.post.$add({
                    text: $scope.comment,
                    image: $scope.image
                }).then(function(text) {
                    console.log('Post Added');
                }).catch(function(error) {
                    console.log(error);
                })
        });
    };
});

现在正在添加帖子,但是图像虽然说正在上传......它不是......谁能告诉我在这方面做错了什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多