我找到了这个nodejs包,并且我有正确结果的工作代码。但是,我不知道如何将这些结果传递给其他函数。我想要的是:从网页中获取一些信息,将其存储在变量中,传递给onesignal函数并发送一个onesignal通知(我使用的是onesignal-node包)。这是我现在的代码:
osmosis
.get('url')
.find('.content_body_left li')
.set('day')
.follow('a@href')
.find('table tr')
.delay(1000)
.set({
city: 'td[2]',
street: 'td[3]',
time:'td[4]'
})
.data(function(document) {
var city = document['city']; // <== this variable I want to get
return city;
})
var firstNotification = new OneSignal.Notification({
contents: {
// en: "test msg"
en: city // <== this is where I want to put variable
}
});问题是我不能从.data(function(document) { ... })中获取document['city'];值,所以我不能在代码中的任何地方使用它。
编辑:我在.data(function(document) { ... })之后也有这段代码
firstNotification.setIncludedSegments(['All']);
firstNotification.setExcludedSegments(['Inactive Users']);
firstNotification.setParameter('data', {"abc": "123", "foo": "bar"});
myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) {
console.log('Something went wrong...');
} else {
console.log(data, httpResponse.statusCode);
}
});所以,如果我放入var firstNotification = new OneSignal.Notification({ ... });,它会说:"firstNotification没有定义“。但是如果我把所有这些代码放在.data(function(document) { ... })或promise中,它就能正常工作。但是,有没有办法将另一半代码排除在.data (或promise)之外?
发布于 2018-02-25 19:22:16
您可以将逻辑包装在Promise中以获取city:
var promise = new Promise(resolve => {
osmosis
.get('url')
.find('.content_body_left li')
.set('day')
.follow('a@href')
.find('table tr')
.delay(1000)
.set({
city: 'td[2]',
street: 'td[3]',
time:'td[4]'
})
.data(function(document) {
var city = document['city']; // <== this variable I want to get
resolve(city);
})
});
promise.then(city => {
var firstNotification = new OneSignal.Notification({
contents: {
// en: "test msg"
en: city // <== this is where I want to put variable
}
});
})发布于 2018-02-25 19:19:26
试一试
osmosis
.get('url')
.find('.content_body_left li')
.set('day')
.follow('a@href')
.find('table tr')
.delay(1000)
.set({
city: 'td[2]',
street: 'td[3]',
time:'td[4]'
})
.data(function(document) {
var firstNotification = new OneSignal.Notification({
contents: {
// en: "test msg"
en: document.city // <== this is where I want to put variable
}
});
})https://stackoverflow.com/questions/48972770
复制相似问题