Search code examples
javascriptobject-reference

a javascript Object reference bug


I meet a bug like this:

    let     needUploadFiles: string[] = [],
            needUploadPages: Filmstrip[] =[],
            needUploadContents: string[] = [];


        Win.dataSource.filmstrips.forEach((filmstrip: Filmstrip, index) => {

            if (Page.pages[index]) {
                filmstrip.content = Page.pages[index].cleanHTML();
            }   

            let hash = Lib.sha1(filmstrip.content),
                content: string = filmstrip.content;

            if (filmstrip.hash != hash) {
                needUploadFiles.push(hash);
                needUploadPages.push(filmstrip);
                needUploadContents.push(content);

            }

        });

Promise.uploadFiles(
                needUploadFiles, needUploadContents,
                (hash: string, index: number) => {
                    needUploadPages[index].hash = hash;
                }
            );

I want change Win.dataSource.filmstrips.hash;

usually it ok.

but sometimes needUploadPages[index].hash can not change Win.dataSource

I add console in Promise.uploadFiles like:

i change Win.dataSource.filmstrips,

make Win.dataSource.filmstrips.length = 1 to check;

When a bug occurs :

Promise.uploadFiles(
                    needUploadFiles, needUploadContents,
                    (hash: string, index: number) => {
                        console.log(hash);
                        needUploadPages[index].hash = hash;
                        console.log(needUploadPages[index].hash == hash)//true
                        console.log(Win.dataSource.filmstrips[index].hash == hash)//false
                        console.log(Win.dataSource.filmstrips[index] === needUploadPages[index])// false
                    }
                );

then i changed like this:

            let needUploadPages: Filmstrip[] =[],
            needUploadContents: string[] = [],
            needUploadIndex: number[] = [];

        Win.dataSource.filmstrips.forEach((filmstrip: Filmstrip, index) => {

            if (Page.pages[index]) {
                filmstrip.content = Page.pages[index].cleanHTML();
            }   

            let hash = Lib.sha1(filmstrip.content),
                content: string = filmstrip.content;

            if (filmstrip.hash != hash) {

                needUploadPages.push(filmstrip);
                needUploadContents.push(content);
                needUploadIndex.push(index);
            }

        });

Promise.uploadFiles(
                needUploadFiles, needUploadContents,
                (hash: string, index: number) => {

               Win.dataSource.filmstrips[needUploadIndex[index]].hash = hash;

                }
            );

it not bug.

I don't know why.

why sometime is right sometime false;

I hope get this answer.

thank you guys,

My English is not good,sorry.


Solution

  • Object references work fine. Try this simple example:

    const origObjects = [{a: 1}, {a: 2}, {a: 3}]
    const newObjects = []
    
    origObjects.forEach(obj => newObjects.push(obj))
    newObjects[1].a = 55
    console.log(origObjects[1]) // Results in {a: 55}
    

    I would say you have an error somewhere in the arrays and indexes. Try console.log the arrays and compare them whether they have the same content.