Uncaught Error: Reference.push failed:

Uncaught Error: Reference.push failed:

所以我使用 cryptojs 和 firebase 发送加密消息,然后在聊天框中显示该加密消息。我可以在没有任何加密的情况下发送常规消息,但当我加密消息时。我最终收到此错误:

未捕获错误:Reference.push 失败:第一个参数包含 属性 'messages.text.init' 中的函数,其内容 = function () { subtype.$super.init.apply(this, arguments);

我想因为我正在推动消息的加密,所以它是一个函数。 不过不确定。

    messageForm.addEventListener("submit", function (e) {
    e.preventDefault();

    var user = auth.currentUser;
    var userId = user.uid;
    if (user.emailVerified) {
        // Get the ref for your messages list
        var messages = database.ref('messages');

        // Get the message the user entered
        var message = messageInput.value;

        var myPassword = "11111";
        var myString = CryptoJS.AES.encrypt(message, myPassword);

        // Decrypt the after, user enters the key
        var decrypt = document.getElementById('decrypt')

        // Event listener takes input
        // Allows user to plug in the key
        // function will decrypt the message
        decrypt.addEventListener('click', function (e) {
            e.preventDefault();
            // Allows user to input there encryption password 
            var pass = document.getElementById('password').value;

            if (pass === myPassword) {
                var decrypted = CryptoJS.AES.decrypt(myString, myPassword);

                document.getElementById("demo0").innerHTML = myString;
                // document.getElementById("demo1").innerHTML = encrypted;
                document.getElementById("demo2").innerHTML = decrypted;
                document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
            }
        });

        // Create a new message and add it to the list.
        messages.push({
                displayName: user.displayName,
                userId: userId,
                pic: userPic,
                text: myString,
                timestamp: new Date().getTime() // unix timestamp in milliseconds

            })
            .then(function () {
                messageStuff.value = "";

            })
            .catch(function (error) {
                windows.alert("Your message was not sent!");
                messageStuff;
            });

看这行代码:

var myString = CryptoJS.AES.encrypt(message, myPassword);

myString 不是字符串。我相信这是一个 CipherParams 对象。 (Reading from the documentation here.) 然后你试图使该对象成为数据库中的一个字段:

messages.push({
        displayName: user.displayName,
        userId: userId,
        pic: userPic,
        text: myString,
        timestamp: new Date().getTime() // unix timestamp in milliseconds
})

这行不通。您需要在那里存储一个字符串而不是一个对象。尝试使用 encrypt() 的 return 值调用 toString() 来存储一个字符串,稍后您可以将其转换回您需要的任何内容:

messages.push({
        displayName: user.displayName,
        userId: userId,
        pic: userPic,
        text: myString.toString(),
        timestamp: new Date().getTime() // unix timestamp in milliseconds
})