“luwak” Kode Jawaban

luwak

$ npm install mongoose --save
Cute Crab

luwak

npm i mongoose // install npm package first
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
// create user
const User = mongoose.model('User', { name: String });
const kitty = new User({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
CodePadding

luwak

const mongoose = require("mongoose");

// use .env to connect 

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    console.log(`mongodb connected :${conn.connection.host}`);
  } catch (error) {
    console.error(`Error:${error.message}`);
    process.exit();
  }
};

module.exports = connectDB;

---------Create Model or Collection-----------
import { Schema, model } from "mongoose"

let userSchema = new Schema({
    name: { type: String, required: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true, },
    role: { type: "String", default: "customer" }
})

export const userModel = model("User", userSchema, "users")
Rasel Hossain

luwak

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});

const Cat = mongoose.model('Cat', { name: String });

const kitty = new Cat({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
Fair Frog

luwak

// schema creation...

let modal;
SchemaVariable.pre('save', function(next) {
  if (this.isNew) {
    if (!modal) {
      modal = mongoose.model('collectionname');
    }
    modal.find({})
      .then((entries) => {
        this._id = entries.length + 1;
        next();
      })
  }
});
Stupid Someone

luwak

mongoose
    .connect("mongodb://localhost:27017/blogDB", {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    .then(() => console.log("Database connected!"))
    .catch(err => console.log(err));
Clumsy Crane

luwak

const adirelad = "a"; 
Upset Unicorn

luwak

no, he doesn't say among us
Despedite

Jawaban yang mirip dengan “luwak”

Pertanyaan yang mirip dengan “luwak”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya