如果来自 mongoDB 的值为空,则进行 api 调用
Make api call if value from mongoDB is null
我正在开发一个应用程序,我正在使用 MEAN 堆栈。我使用 MongoDB 将值保存在数据库中。我想使用这些值进行 API 调用,但前提是值为 null。现在它成功地从数据库中获取了这些值并调用了 API 但我不知道如何检查该值是否为空,我基本上已经尝试了所有方法但我想我不完全理解NodeJS 并且它是异步的。这是有效但不检查值是否为空的代码:
var makeApiCalls = function (workerId) {
var Model = mongoose.model('Tweet'.concat(workerId), Tweet.tweetSchema);
return Model.find({},{ _id: 1, message: 1}).then(messages =>
Promise.all(
messages.map(({ _id, message }) =>
api.sentiment(message).then(result =>
Model.findOneAndUpdate({ _id }, { resultOfTheCall: result }, { new: true })
.then( updated => { console.log(updated); return updated })
)
)
)
)
};
mongoose 模型有一个名为 resultOfTheCall 的字段,我需要检查该值是否为 null,只有在这种情况下,我想使用字段 message 调用 API。
这是console.log其中一条消息:
{
_id: 5b85c83b413a2b1473e7122a,
date: 'Tue Aug 28 22:10:02 +0000 2018',
message: 'la gente quiere y no viene',
resultOfTheCall: 0.5931016939587707,
__v: 0 }
在制作 Promise.all
之前简单地过滤消息。
var makeApiCalls = function(workerId) {
var Model = mongoose.model('Tweet'.concat(workerId), Tweet.tweetSchema);
return Model.find({}, {
_id: 1,
message: 1,
resultOfTheCall: 1
}).then(messages => {
// filter the mssages here those for which to make the call and the others
var toCallArray = messages.filter(x => x.resultOfTheCall == null)
var noCallArray = messages.filter(x => x.resultOfTheCall != null)
// now do the calls for only those which do not have resultOfTheCall
return Promise.all(
toCallArray.map(({_id, message}) =>
api.sentiment(message).then(result =>
Model.findOneAndUpdate({
_id
}, {
resultOfTheCall: result
}, {
new: true
})
.then(updated => {
console.log(updated);
return [...noCallArray, ...toCallArray]
})
)
)
)
})
};
我正在开发一个应用程序,我正在使用 MEAN 堆栈。我使用 MongoDB 将值保存在数据库中。我想使用这些值进行 API 调用,但前提是值为 null。现在它成功地从数据库中获取了这些值并调用了 API 但我不知道如何检查该值是否为空,我基本上已经尝试了所有方法但我想我不完全理解NodeJS 并且它是异步的。这是有效但不检查值是否为空的代码:
var makeApiCalls = function (workerId) {
var Model = mongoose.model('Tweet'.concat(workerId), Tweet.tweetSchema);
return Model.find({},{ _id: 1, message: 1}).then(messages =>
Promise.all(
messages.map(({ _id, message }) =>
api.sentiment(message).then(result =>
Model.findOneAndUpdate({ _id }, { resultOfTheCall: result }, { new: true })
.then( updated => { console.log(updated); return updated })
)
)
)
)
};
mongoose 模型有一个名为 resultOfTheCall 的字段,我需要检查该值是否为 null,只有在这种情况下,我想使用字段 message 调用 API。
这是console.log其中一条消息:
{
_id: 5b85c83b413a2b1473e7122a,
date: 'Tue Aug 28 22:10:02 +0000 2018',
message: 'la gente quiere y no viene',
resultOfTheCall: 0.5931016939587707,
__v: 0 }
在制作 Promise.all
之前简单地过滤消息。
var makeApiCalls = function(workerId) {
var Model = mongoose.model('Tweet'.concat(workerId), Tweet.tweetSchema);
return Model.find({}, {
_id: 1,
message: 1,
resultOfTheCall: 1
}).then(messages => {
// filter the mssages here those for which to make the call and the others
var toCallArray = messages.filter(x => x.resultOfTheCall == null)
var noCallArray = messages.filter(x => x.resultOfTheCall != null)
// now do the calls for only those which do not have resultOfTheCall
return Promise.all(
toCallArray.map(({_id, message}) =>
api.sentiment(message).then(result =>
Model.findOneAndUpdate({
_id
}, {
resultOfTheCall: result
}, {
new: true
})
.then(updated => {
console.log(updated);
return [...noCallArray, ...toCallArray]
})
)
)
)
})
};