TypeError:无法读取未定义的属性“创建”

Posted

技术标签:

【中文标题】TypeError:无法读取未定义的属性“创建”【英文标题】:TypeError: Cannot read property 'create' of undefined 【发布时间】:2020-05-28 22:26:48 【问题描述】:

嗨,我刚开始使用 mongoose,我正在尝试运行我的代码的一部分,但它不能正常工作,每件事看起来都很好,但它一直给我错误无法读取 unfriend 的属性“创建”我尝试过其他语法但我得到同样的错误请帮助meee ...我将在这里发送代码,这两个console.log不起作用console.log(“helooooo”),console.log(dbArticle),代码给了我我会的错误将架构发送到此处,并且我最后在 ui 上得到 res.send。最后我应该提到我把这行代码放在我的 server.js 文件中 const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/scraper"; mongoose.connect(MONGODB_URI); 因为我想在 heroku 上部署我的应用程序

const mongoose = require("mongoose");

// Save a reference to the Schema constructor
const Schema = mongoose.Schema;

// Using the Schema constructor, create a new UserSchema object
// This is similar to a Sequelize model
const ArticleSchema = new Schema(
  // `title` is required and of type String
  title: 
    type: String,
    unique: true,
    required: true
  ,
  // `link` is required and of type String
  link: 
    type: String,
    required: true
  ,

  summary: 
    type: String,
    required: true
,

image: 
    type: String,
    required: true
,

saved: 
    type: Boolean,
    default: false
,

  // `note` is an object that stores a Note id
  // The ref property links the ObjectId to the Note model
  // This allows us to populate the Article with an associated Note
  note: [
    type: Schema.Types.ObjectId,
    ref: "Note"
  ]
);

// This creates our model from the above schema, using mongoose's model method
const Article = mongoose.model("Article", ArticleSchema);

// Export the Article model
module.exports = Article;

const express = require ("express");
const axios = require ("axios");
const cheerio = require ("cheerio");
const mongoose = require("mongoose");
const db = require ("../models");

module.exports = function (app) 


    app.get ("/scrape", function (req, res)
        axios.get("https://www.newsweek.com/").then (function(response)
            const $ = cheerio.load(response.data);
    
            $("article.clearfix").each (function (i, element) 

                 // Save an empty result object
    
                let result = ;

                 // Add the text, summary, image and href of every link, and save them as properties of the result object
                result.title = $(this).children("h4").children("a").text();
                result.link = $(this).children("h4").children("a").attr("href");
                result.image = $(this).children(".image").children("a").children("picture").children("source").attr("srcset")
                result.summary = $(this).children(".summary").text();

                console.log(result)
    
               db.Article.create(result).then( function(dbArticle) 
                   console.log("helooooo")
                console.log(dbArticle)
               ).catch(function (err) 
                console.log(err)
              )
              
            );
        );
        res.send("it's working yeaaaaa")
    );

【问题讨论】:

【参考方案1】:

后端的服务器代码在哪里?

【讨论】:

以上是关于TypeError:无法读取未定义的属性“创建”的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:无法读取未定义的属性“创建”

Vue警告:创建的钩子出错:“TypeError:无法读取未定义的属性'get'”

TypeError:无法读取未定义的属性“getters”

未捕获(承诺):TypeError:无法读取未定义的属性“创建”(离子 3.9,Angularjs 5.0.3)

(节点:47028)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“表情符号”

NextJS:TypeError:无法读取未定义的属性“json”