簡體   English   中英

無法讀取未定義的屬性“類型” —使用貓鼬Schema.Types.ObjectId

[英]Cannot read property 'Types' of undefined — using mongoose Schema.Types.ObjectId

目前的行為是什么?

為了運行腳本,我使用babel-node因為腳本使用es6。

Cannot read property 'Types' of undefined

authorId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
                                   ^

如果當前行為是錯誤,請提供重現步驟。 我已經定義了這樣的模式:

import * as mongoose from 'mongoose';

const Schema = mongoose.Schema;

const RecipeSchema = new Schema ({
  authorId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
  name: String,
  description: String,
  photos: [
    {
      name: String,
      path: String,
      isMain: Boolean,
      alt: String
    }
  ],
  ingredients: [
    {
      name: String,
      quantity: Number,
      metricType: {
        type: String,
        enum: [ 'kg', 'g', 'mg', 'l', 'ml', 'unit' ],
        default: 'unit'
      }
    }
  ],
  preparement: String,
  isForSell: { type: Boolean, required: true, default: false },
  price: Number,
  url: String,
  portionNumber: Number,
  time: Number,
  grades: [
    {
      user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
      grade: Number,
      date: Date
    }
  ],

} );

export default mongoose.model ( 'Recipe', RecipeSchema );

並嘗試使用以下函數為數據庫播種:

async function insert_recipe () {

  const user = await User.findOne ( {} );

  await Recipe.create ( {
    authorId: user.id,
    name: 'some name',
    description: 'some description',
    ingredients: [
      {
        name: 'foo',
        quantity: 12,
        metricType: 'g'
      },
      {
        name: 'bar',
        quantity: 50,
        metricType: 'g'
      }
    ],
    preparement: 'how to do something like this',
    isForSell: false,
    portionNumber: '5',
    time: 20

預期的行為是什么? 它應該使用擁有配方的第一個用戶的ID,並在數據庫上自己創建配方。

請提及您的node.js,mongoose和MongoDB版本。 我目前正在使用所有這些的最新版本。 (2017-09-15)

經過幾次試驗后,我發現Schema代碼中有一些更改的解決方案。

import * as mongoose from 'mongoose';
import { Schema, model } from 'mongoose';
import User from '../models/user';

const RecipeSchema = new Schema ({
  authorId: { type: Schema.Types.ObjectId, ref: 'User' },
  name: String,
  description: String,
  photos: [
    {
      name: String,
      path: String,
      isMain: Boolean,
      alt: String
    }
  ],
  ingredients: [
    {
      name: String,
      quantity: Number,
      metricType: {
        type: String,
        enum: [ 'kg', 'g', 'mg', 'l', 'ml', 'unit' ],
        default: 'unit'
      }
    }
  ],
  preparement: String,
  isForSell: { type: Boolean, required: true, default: false },
  price: Number,
  url: String,
  portionNumber: Number,
  time: Number,
  grades: [
    {
      user: { type: Schema.Types.ObjectId, ref: 'User' },
      grade: Number,
      date: Date
    }
  ],

} );

export default model.call(require('mongoose'), 'Recipe', RecipeSchema);

所以我基本上是直接導入Schemamodel ,而不是與mongoose.Schemamongoose.model

最后,我還必須用model進行調用以引用mongoose ,例如model.call(require('mongoose'), 'Recipe', RecipeSchema);

現在一切正常。

謝謝順便說一句!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM