javascript - 赋值nodejs中的左侧无效

标签 javascript node.js

嘿,这让我发疯了。我有这段代码,期望的目标是创建一个具有给定属性的作者。我已经运行了 mongodb compass 和 mongod 等所有内容,在出现此错误后我更新了 npm + node。仍然是一个错误。

代码在这里:

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/playground')
  .then(() => console.log('Connected to MongoDB...'))
  .catch(err => console.error('Could not connect to MongoDB...', err));

const authorSchema = new mongoose.Schema({
   name: String,
   bio: String,
   website: String,
})

const courseSchema = new mongoose.Schema({
   name: String
})

const Author = mongoose.model('Author', authorSchema)

const Course = mongoose.model('Course',courseSchema)

async function createAuthor(name, bio, website) { 
  const author = new Author({
    name, 
    bio, 
    website 
  });

  const result = await author.save();
  console.log(result);
}

async function createCourse(name, author) {
  const course = new Course({
    name, 
    author
  }); 

  const result = await course.save();
  console.log(result);
}

async function listCourses() { 
  const courses = await Course
    .find()
    .select('name');
  console.log(courses);
}

createAuthor('Mosh', 'My bio', 'My Website');

// createCourse('Node Course', 'authorId')

// listCourses();

给定的错误在这里:

|| (selector['mapreduce'] && selector.out = 'inline')) {
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ReferenceError: Invalid left-hand side in assignment
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at /Users/eesamunir/node_modules/mongodb/lib/mongodb/index.js:29:17

最佳答案

由于错误没有出现在您的代码中,因此它必须是来自 mongoose 的错误。看起来 SyntaxError 已经被修复了,所以只需使用以下命令获取新的修复版本:

  npm install mongoose

关于javascript - 赋值nodejs中的左侧无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250035/

相关文章:

node.js - 在没有端口的情况下运行 Node js 应用程序

node.js - NodeJS/Webpack Heroku 部署的端口超时

javascript - if javascript 中 .datepicker 属性的条件

javascript - 如何强制用户选择至少一个单选框

javascript - 跨浏览器问题: How do I debug my site's behavior on safari with window PC?

mysql - 使用 sequelize 等待查询

javascript - 设置 karma 来测试 node.js 模块?

javascript - 使用 jQuery 有选择地遍历 div

javascript - 如果验证失败则停止表单提交。

node.js - 导出 Typescript 模块 Nodejs 中的所有内容