• Adding Images


    refer to: https://www.udemy.com/course/the-web-developer-bootcamp

    image source: https://source.unsplash.com/

    models/campground.js

    const mongoose = require('mongoose'); const Schema = mongoose.Schema; const CampgroundSchema = new Schema({ title: String, image: String, Price: Number, description: String, location: String }); module.exports = mongoose.model('Campground', CampgroundSchema);
    seeds/index.js

    const mongoose = require('mongoose'); const cities = require('./cities'); const { places, descriptors } = require('./seedHelpers'); const Campground = require('../models/campground'); // ".." to get outside the seeds folder mongoose.connect('mongodb://localhost:27017/yelp-camp', { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true }); const db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function () { console.log("Database connected"); }); // array[Math.floor(Math.random() * array.length)], 产生0~数组长度-1的随机数 const sample = array => array[Math.floor(Math.random() * array.length)]; const seedDB = async () => { await Campground.deleteMany({});//删除之前的记录 for (let i = 0; i < 50; i++) { const random1000 = Math.floor(Math.random() * 1000);//包含1000个城市, 0-999的随机数 const price = Math.floor(Math.random() * 20) + 10; const camp = new Campground({ location: `${cities[random1000].city}, ${cities[random1000].state}`, title: `${sample(descriptors)} ${sample(places)}`, image: 'https://source.unsplash.com/collection/483252/400x300', description: 'so beautiful', price }) await camp.save(); } } //一定要记得运行这个! //运行之后断开mongoose连接 seedDB().then(() => { mongoose.connection.close(); })
    views.show.ejs
    
    <% layout('layouts/boilerplate') %>
    
        <h1>
            <%=campground.title%>
        </h1>
        <h2>
            <%=campground.location%>
        </h2>
        <img src="<%= campground.image%>" alt="">
        <p>
            <%= campground.description%>
        </p>
        <p>
            <a href="/campgrounds/<%=campground._id%>/edit">Edit</a>
    
        </p>
        <p>
        <form action="/campgrounds/<%=campground._id%>?_method=DELETE" method="POST">
            <button>delete</button>
        </form>
        </p>
        <footer>
            <a href="/campgrounds">All</a>
        </footer>
        <!-- a link to return to the all campgrounds page -->

  • 相关阅读:
    docker-redis
    docker-nginx
    docker-tomcat
    JQuery/JS插件 jsTree加载树,预先加载,初始化时加载前三级节点,当展开第三级节点时 就加载该节点下的所有子节点
    Python json
    Python 模拟鼠标
    Python 取列表的前几个
    winfrom 图片等比例压缩
    winfrom 改变图片透明度 Alpha
    winform 实现类似于TrackBar的自定义滑动条,功能更全
  • 原文地址:https://www.cnblogs.com/LilyLiya/p/14401467.html
Copyright © 2020-2023  润新知