-2

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.

1 Answers1

0

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.

Jan Steuer
  • 141
  • 4
  • thank you.but when Win.dataSource.filmstrips.length = 1 ,also had this bug,i had edit this question and add console. – zhaoxuan du Apr 12 '18 at 08:28
  • Try print `console.log(Win.dataSource.filmstrips[index])` and `console.log(needUploadPages[index])` whether there isn't some nonsense in one of the arrays. – Jan Steuer Apr 12 '18 at 08:56
  • @ Jan Steuer, thank you i try. but only diffrent on hash. – zhaoxuan du Apr 12 '18 at 09:07
  • And what is `Win.dataSource`? Don't you reload it somewhere? Because it would create new objects. Try to update the hash in the `forEach` and compare it there. – Jan Steuer Apr 12 '18 at 09:19