Skip to content

how can return values be guaranteed? #265

Description

@tripower

is it possible that a return value is guaranteed?
because if one "find" doesn not find anything the whole data is empty
so I tried this code but it only returns the first value??

const osmosis = require('osmosis');
let savedData = [];

var objSite = osmosis.get('https://www.wallstreet-online.de/etf/a0rpwh-ishares-core-msci-world-ucits-etf');
objSite.find("//*[@class='marketchange'][1]/div[1]/div[1]/div[1]/span")
    .set({'kurs': 'text()'})
    .data(data => {
        console.log("data1:" + data);
        savedData.push(data);
        objSite.find("//*[@class='marketchange'][1]/div[1]/div[3]/span")
                .set({'perc': 'text()'})
                .data(data => {
                      console.log("data2:" + data);
                      savedData.push(data);
                })
			    .done(function() {
				    var strFileData = JSON.stringify( savedData, null, 4);
				    console.log("strFileData:" + strFileData);
				    fs.writeFile('data.json', strFileData, function(err) {
					 if(err) console.error(err);
					 else console.log('Data Saved to data.json file');
				   })
				});
    });

=> output
strFileData:[
{
"kurs": "71,39"
}
]

original example which works if all xpath queries find something

    objSite.find("//*[@class='marketchange'][1]/div[1]/div[1]/div[1]/span")
    .set({'kurs': 'text()'})
	.find("//*[@class='marketchange'][1]/div[1]/div[3]/span")
	.set({'perc': 'text()'})
	.data(data => {
	  console.log("data2:" + data);
	  savedData.push(data);
	})
	.done(function() {
		var strFileData = JSON.stringify( savedData, null, 4);
		console.log("strFileData:" + strFileData);
		fs.writeFile('data.json', strFileData, function(err) {
		 if(err) console.error(err);
		 else console.log('Data Saved to data.json file');
	   })
	});

=> output
strFileData:[
{
"kurs": "71,39",
"perc": "+0,08"
}
]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions