首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >解析链中的promise

解析链中的promise
EN

Stack Overflow用户
提问于 2018-06-10 01:26:16
回答 1查看 69关注 0票数 0

我在解析链中的承诺时遇到了一些问题。在下面的JSBin中,你可以看到我的代码(NodeJS和mongo)。如何从链中的2个级别返回数据?以下是代码和JSBIN

function test(url) {
  return new Promise((resolve, reject) => {
  return scrapeIt({
    url: url,
    headers: { 'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36' }
  }, {
      urls: {
        listItem: '#list',
        data: {
          url: {
            selector: "span[data-label='url']"
          }
        }
      },
      currentPage: {
        selector: 'span.current',
        convert: x => parseInt(x)
      },
      pages: {
        selector: 'span.pages',
        convert: x => parseInt(x)
      }
    }).then(({ data, response }) => {
      return MongoClient.connect(config.db.serverUrl, function (err, db) {
        var dbo = db.db(config.db.name)
        var urlsToInsert = data.urls

        return dbo.collection(config.db.collection).find({ 'url': { '$in': data.urls } }, { projection: { _id: 0 } }).toArray(function (err, res) {
          if (res && res.length > 0) {
            // Exclude properties already in DB
            urlsToInsert = urlsToInsert.filter(x => !res.some(y => x.url == y.url))
            if (urlsToInsert && urlsToInsert.length > 0)
              return dbo.collection(config.db.collection).insertMany(urlsToInsert, function (err, res) {
                console.log("Number of documents inserted: " + res.insertedCount)

                db.close()
                return urlsToInsert
              })
          }
        })
      })
    })
})
}

test('https://www.google.com').then(({ data, response }) => {
  console.log(data)
}).catch((error) => {
  console.log(error)
})

JSBin snippet

更新1

我的实际代码要长得多,所以我去掉了很多不需要的代码,以使问题变得有意义。我修复了urlsToInsert为空的问题。现在也返回urlsToInsert。

EN

回答 1

Stack Overflow用户

发布于 2018-06-10 02:13:53

我曾尝试重写最后一部分,但您的代码无法工作,因为您将urlsToInsert设置为一个空数组,然后对其进行过滤。

您返回的propertiesToInsert在代码中从未定义过。

.then(
  ({ data, response }) =>
    new Promise(
      (resolve,reject)=>
        MongoClient.connect(config.db.serverUrl,(err, db) =>
          (err, db) =>
            (err)
              ? reject(err)
              : resolve(db.db(config.db.name))
        )
    )
).then(
  dbo=>
    new Promise(
      (resolve,reject)=>
        dbo.collection(config.db.collection).find(
          { 'url': { '$in': data.urls } },
          { projection: { _id: 0 } }
        ).toArray((err, res) =>
          (err)
            ? reject(err)
            : resolve(res)
        )
    ).then(
      res=>{
        var urlsToInsert = [];
        if (res && res.length > 0) {
          // Exclude properties already in DB THIS MAKES NO SENSE, FILTERING AN EMPTY ARRAY
          urlsToInsert = urlsToInsert.filter(x => !res.some(y => x.url == y.url))
          if (urlsToInsert && urlsToInsert.length > 0) {
            return new Promise(
              (resolve,reject)=>
                dbo.collection(config.db.collection).insertMany(urlsToInsert, function (err, res) {
                  if(err){reject(err);}//this never happens anyway
                  console.log("Number of documents inserted: " + res.insertedCount);    
                  db.close();
                  //what are you returning here?
                  resolve(propertiesToInsert)
                })
            )
          } 
          return [];//what to return if there are no urls to insert (always because you're filtering empty array)
        }
      }
    )
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50776823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档