javascript - Mongoose 类型错误: findOneAndUpdate is not a function

标签 javascript node.js mongodb mongoose puppeteer

我正在以 mongoose 为例学习 Puppeteer( https://medium.com/@e_mad_ehsan/getting-started-with-puppeteer-and-chrome-headless-for-web-scraping-6bf5979dee3e )。 当我执行“node index.js”时,我收到这样的 findOneAndUpdateerror 消息;

(node:53135) DeprecationWarning: current URL string parser is deprecated, 
and will be removed in a future version. To use the new parser, pass option 
{ useNewUrlParser: true } to MongoClient.connect.

(node:53135) UnhandledPromiseRejectionWarning: TypeError: 
User.findOneAndUpdate is not a function**
at upsertUser (/home/oceanm/thal/index.js:111:14)
at run (/home/oceanm/thal/index.js:69:4)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)

(node:53135) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
This error originated either by throwing inside of an async function without 
a catch block, or by rejecting a promise which was not handled with 
.catch(). (rejection id: 1)

(node:53135) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.

...... ...................... ............ ...................... …………

我不知道为什么会收到此错误消息。我被困了几个小时。请帮助我。

<小时/>
<models/user.js>


const mongoose = require('mongoose');

let userSchema = new mongoose.Schema({
    username: String,
    email: String,
    dateCrawled: Date
});
<小时/>
<creds.js>


module.exports = {
    username: 'myid',
    password: 'mypassword'
}
<小时/>
<index.js>

const puppeteer = require('puppeteer');
const CREDS = require('./creds');
const mongoose = require('mongoose');
const User = require('./models/user');

async function run() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto('https://github.com/login');

const USERNAME_SELECTOR = '#login_field';
const PASSWORD_SELECTOR = '#password';
const BUTTON_SELECTOR = '#login > form > div.auth-form-body.mt-3 > input.btn.btn-primary.btn-block';

await page.click(USERNAME_SELECTOR);
await page.keyboard.type(CREDS.username);

await page.click(PASSWORD_SELECTOR);
await page.keyboard.type(CREDS.password);

await page.click(BUTTON_SELECTOR);

await page.waitForNavigation();

const userToSearch = 'miyamoto';
const searchUrl = `https://github.com/search?q=${userToSearch}&type=Users`;
await page.goto(searchUrl);
await page.waitFor(2*1000);

const LIST_USERNAME_SELECTOR = '#user_search_results > div.user-list > div:nth-child(INDEX) > div.d-flex.flex-auto > div > a';
const LIST_EMAIL_SELECTOR = '#user_search_results > div.user-list > div:nth-child(INDEX) > div.d-flex.flex-auto > div > ul > li:nth-child(2) > a';
const LENGTH_SELECTOR_CLASS = 'user-list-item';

let numPages = await getNumPages(page);

console.log('Numpages: ', numPages);

for (let h = 1; h <= numPages; h++) {

let pageUrl = searchUrl + '&p=' + h;
await page.goto(pageUrl);

let listLength = await page.evaluate((sel) => {
    return document.getElementsByClassName(sel).length;
  }, LENGTH_SELECTOR_CLASS);

for (let i = 1; i <= listLength; i++) {
    // change the index to the next child
    let usernameSelector = LIST_USERNAME_SELECTOR.replace("INDEX", i);
    let emailSelector = LIST_EMAIL_SELECTOR.replace("INDEX", i);

    let username = await page.evaluate((sel) => {
        return document.querySelector(sel).getAttribute('href').replace('/', '');
      }, usernameSelector);

    let email = await page.evaluate((sel) => {
        let element = document.querySelector(sel);
        return element? element.innerHTML: null;
      }, emailSelector);

    // not all users have emails visible
    if (!email)
      continue;

    console.log(username, ' -> ', email);

    // TODO save this user
   upsertUser({
   username: username,
   email: email,
   dateCrawled: new Date()
   });

}
}
  browser.close();
}

async function getNumPages(page) {
  const NUM_USER_SELECTOR = '#js-pjax-container > div > div.col-12.col-md-9.float-left.px-2.pt-3.pt-md-0.codesearch-results > div > div.d-flex.flex-column.flex-md-row.flex-justify-between.border-bottom.pb-3.$

  let inner = await page.evaluate((sel) => {
    let html = document.querySelector(sel).innerHTML;

    return html.replace(',', '').replace('users', '').trim();
  }, NUM_USER_SELECTOR);

  let numUsers = parseInt(inner);

  console.log('numUsers: ', numUsers);

  let numPages = Math.ceil(numUsers / 10);
  return numPages;
}



async function upsertUser(userObj) {

        const DB_URL = 'mongodb://localhost/thal';

        if (mongoose.connection.readyState == 0) { mongoose.connect(DB_URL); }

        // if this email exists, update the entry, don't insert
        let conditions = { email: userObj.email };
        let options = { upsert: true, new: true, setDefaultsOnInsert: true };

        User.findOneAndUpdate(conditions, userObj, options, (err, result) => {
                if (err) throw err;
        });
}



run();

最佳答案

我认为您没有导出 User型号 <models/user.js>

添加module.exports = mongoose.model('User', userSchema);使用User没有它的变量可能会导致这个问题。

const mongoose = require('mongoose');

let userSchema = new mongoose.Schema({
    username: String,
    email: String,
    dateCrawled: Date
});

module.exports = mongoose.model('User', userSchema);

关于javascript - Mongoose 类型错误: findOneAndUpdate is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51832225/

相关文章:

c# - MongoDB 的存储库模式 - 一个事务的多个工作单元

json - Mongodb:根据另一个元素重命名数组中的json元素

Javascript 变量作用域返回未定义

javascript - 如何从 Chrome 扩展中的弹出窗口访问后台页面中的对象

javascript - 请求和响应对象在移动到函数时变得未定义(nodejs,express)

javascript - 如何按时间顺序运行 Mocha 测试?

javascript - 我很奇怪 "Async"模块不能正常工作

node.js - 如何仅使用电子邮件 passport.js 对用户进行身份验证?

java - 带有java的Mongodb - 查找批量大小的查询

javascript - Google map API - results.geometry.location[0] 返回 null