0

我正在关注本教程:http ://rationalappdev.com/api-backend-with-nodejs-express-and-mongodb-for-react-native-apps/

基本上,我正在尝试在 windows 上创建一个 MongoDB 并用 movie.js 文件中的一些数据填充它

我启动命令node_modules/babel-cli/bin/babel-node.js populate.js,但是,我收到一条错误消息,编译错误,第 1 行:

在此处输入图像描述

我用 mongo.exe 命令行 (db.people.save({"name": "son"})) 检查了 MongoDB,它工作正常。

mongo db 端口是 27017

这是 movie.js 代码:

import mongoose from 'mongoose';
import Movie from './models/movie';

const movies = [
  {
    title: 'La La Land',
    poster: 'https://i.imgur.com/po7UezG.jpg',
    genre: 'Drama/Romance',
  },
  {
    title: 'Paterson',
    poster: 'https://i.imgur.com/pE0C9E0.jpg',
    genre: 'Drama/Comedy',
  },
  {
    title: 'Jackie',
    poster: 'https://i.imgur.com/VqUi1sw.jpg',
    genre: 'Drama/Biography',
  },
  {
    title: 'Zoolander 2',
    poster: 'https://i.imgur.com/ejlIijD.jpg',
    genre: 'Comedy',
  },
];

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/movies');

// Go through each movie
movies.map(data => {
  // Initialize a model with movie data
  const movie = new Movie(data);
  // and save it into the database
  movie.save();
});

这是模型/movie.js:

import mongoose, { Schema } from 'mongoose';

    // Define movie schema
    var movieSchema = new Schema({
        title: {
            type: String,
            unique: true,
        },
        poster: String,
        genre: String,
        days: Array,
        times: Array,
    });

    // Export Mongoose model
    export default mongoose.model('movie', movieSchema);

您知道为什么会这样吗,因为我更改了 mongoDB 端口 mongoose.connect('mongodb://127.0.0.1:27017/movies'); 没用,检查 package.json 确保 babel-node.js 在 node-modules/babel-cli/bin 目录下,单独测试 mongodb,没有思路

谢谢

4

0 回答 0