[GH-ISSUE #6] Live proxy: Uncaught (in promise) TypeError: Cannot read property 'push' of undefined #4

Closed
opened 2026-05-23 08:25:05 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @paradis-A on GitHub (Feb 9, 2021).
Original GitHub issue: https://github.com/appy-one/acebase/issues/6

Originally assigned to: @appy-one on GitHub.

I was trying to live proxy using the following your example

//server/index.js
const { AceBaseServer } = require("acebase-server")
const dbname = "mydb"
const server = new AceBaseServer(dbname, {
    host: "192.168.43.50",
    port: 5757,
    authentication: {
        enabled: false,
        allowUserSignup: false,
        defaultAccessRule: "allow",
        defaultAdminPassword: "75sdDSFg37w5",
    },
})
server.on("ready", () => {
    console.log("SERVER ready")
})

but changed a bit of your example. becase i dont want to wrap to async.

//client/index.js
import { AceBaseClient } from "acebase-client"
    const db = new AceBaseClient({
        host: "192.168.43.50",
        port: 5757,
        dbname: "mydb",
        https: false,
    })
    db.ready(() => {
        console.log("Connected successfully")
    })
    let liveChat = {}
    db.ref("chats/chat1")
        .proxy({})
        .then((r) => {
            liveChat = r.value
            liveChat.title = "Live Data Proxies Rock! 🚀"
            liveChat.members = ["ewout", "john", "pete", "jack"]
            liveChat.messages.push({
                from: "ewout",
                text: "Updating a database was never this easy",
            }) //throws: Uncaught (in promise) TypeError: Cannot read property 'push' of undefined
        })
    if (liveChat?.onChange)
        liveChat.onChange(function (val, prev, remoteChange, context) {
            // Handle specific (local or remote) changes:
            if (val.title !== prev.title && remoteChange) {
                console.log(`Title was changed by someone else`)
            }
            if (prev.members.includes("ewout") && !val.members.includes("ewout")) {
                console.log(remoteChange ? `I was kicked out of this chat` : `I stepped out`)
            }
        })

    function sendMessage(text) {
        // Changes made remotely are automatically updated in our liveChat object:
        if (!liveChat.members.includes("ewout")) {
            throw new Error(`Can't send message, I'm not a member anymore!`)
        }
        liveChat.messages.push({
            from: "ewout",
            text,
        })
    }

liveChat.messages.push throws
Uncaught (in promise) TypeError: Cannot read property 'push' of undefined

Originally created by @paradis-A on GitHub (Feb 9, 2021). Original GitHub issue: https://github.com/appy-one/acebase/issues/6 Originally assigned to: @appy-one on GitHub. I was trying to live proxy using the following your example ```javascript //server/index.js const { AceBaseServer } = require("acebase-server") const dbname = "mydb" const server = new AceBaseServer(dbname, { host: "192.168.43.50", port: 5757, authentication: { enabled: false, allowUserSignup: false, defaultAccessRule: "allow", defaultAdminPassword: "75sdDSFg37w5", }, }) server.on("ready", () => { console.log("SERVER ready") }) ``` but changed a bit of your example. becase i dont want to wrap to async. ```javascript //client/index.js import { AceBaseClient } from "acebase-client" const db = new AceBaseClient({ host: "192.168.43.50", port: 5757, dbname: "mydb", https: false, }) db.ready(() => { console.log("Connected successfully") }) let liveChat = {} db.ref("chats/chat1") .proxy({}) .then((r) => { liveChat = r.value liveChat.title = "Live Data Proxies Rock! 🚀" liveChat.members = ["ewout", "john", "pete", "jack"] liveChat.messages.push({ from: "ewout", text: "Updating a database was never this easy", }) //throws: Uncaught (in promise) TypeError: Cannot read property 'push' of undefined }) if (liveChat?.onChange) liveChat.onChange(function (val, prev, remoteChange, context) { // Handle specific (local or remote) changes: if (val.title !== prev.title && remoteChange) { console.log(`Title was changed by someone else`) } if (prev.members.includes("ewout") && !val.members.includes("ewout")) { console.log(remoteChange ? `I was kicked out of this chat` : `I stepped out`) } }) function sendMessage(text) { // Changes made remotely are automatically updated in our liveChat object: if (!liveChat.members.includes("ewout")) { throw new Error(`Can't send message, I'm not a member anymore!`) } liveChat.messages.push({ from: "ewout", text, }) } ``` liveChat.messages.push throws **Uncaught (in promise) TypeError: Cannot read property 'push' of undefined**
Author
Owner

@appy-one commented on GitHub (Feb 10, 2021):

Apparently the proxy example documentation contains a couple mistakes, thanks for reporting.

.push can only be called on an object collection. Because the chat the code is creating does not have the property messages yet, we can't push an object to it. Adding liveChat.messages = {}; before the first push will fix that.

I see you added an if (liveChat.onChange), probably because that also threw an error. The code should be liveChat.onChanged(...). Note the typo in the example code, it must be onChanged

<!-- gh-comment-id:776639041 --> @appy-one commented on GitHub (Feb 10, 2021): Apparently the proxy example documentation contains a couple mistakes, thanks for reporting. ```.push``` can only be called on an object collection. Because the chat the code is creating does not have the property ```messages``` yet, we can't ```push``` an object to it. Adding ```liveChat.messages = {};``` before the first push will fix that. I see you added an ```if (liveChat.onChange)```, probably because that also threw an error. The code should be ```liveChat.onChanged(...)```. Note the typo in the example code, it must be ```onChanged```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/acebase#4
No description provided.