gpt4 book ai didi

javascript - 我似乎无法引用 2 个模式来填充

转载 作者:行者123 更新时间:2023-12-02 17:24:50 26 4
gpt4 key购买 nike

这些是我想引用的两个模式。我希望listingsScheme userId 引用userScheme 的_id。已经花了 2 天的时间来解决这个问题,请帮忙。

var userScheme = new Schema({
email: { type: String },
password: { type: String },
fullName: { type: String },
coverPhoto: { type: String},
profilePhoto: { type: String},
fbUserID: { type: String},
profileExtraInfo: { type: Array},
listings: {type: Schema.Types.ObjectId, ref: "Listings"}

var listingsScheme = new Schema({
category: { type: String, index: true },
title: { type: String },
description: { type: String },
attributes: { type: Array },
nrViews: {type: Number },
photos: { type: Array },
location: { type: Array, index: '2d' },
created: { type: Date, index: true },
updated: { type: Date },
active: { type: Boolean, index: true } ,
userId : { type: Schema.Types.ObjectId, ref : 'User' } ,
fullName : { type: String },
profilePhoto : { type : String},
followers : {type : String},

最佳答案

除了语法错误之外,您的代码应该可以工作。

  1. 请记住使用复数模型名称(您有“User”=>“users”)。
  2. 不要忘记对您的架构进行建模。
  3. 不要忘记使用 .populate('userId') 方法。
  4. 不要忘记连接服务器(这种情况发生的频率比您想象的要高)。
  5. 确保您确实拥有相互关联的文档。

测试代码:

var mongoose  = require('mongoose');

// connect with database.
mongoose.connect('mongodb://localhost:27017/temp');

var userScheme = new mongoose.Schema({
email: { type: String },
password: { type: String },
fullName: { type: String },
coverPhoto: { type: String},
profilePhoto: { type: String},
fbUserID: { type: String},
profileExtraInfo: { type: Array},
listings: {type: mongoose.Schema.Types.ObjectId, ref: "Listings"}
});

var listingsScheme = new mongoose.Schema({
category: { type: String, index: true },
title: { type: String },
description: { type: String },
attributes: { type: Array },
nrViews: {type: Number },
photos: { type: Array },
location: { type: Array, index: '2d' },
created: { type: Date, index: true },
updated: { type: Date },
active: { type: Boolean, index: true } ,
userId : { type: mongoose.Schema.Types.ObjectId, ref : 'User' } ,
fullName : { type: String },
profilePhoto : { type : String},
followers : {type : String}
});

// don't forget to 'model' your scheme.
var users = mongoose.model('User', userScheme);
var listings = mongoose.model('listings', listingsScheme);

// create mock user.
var user = new users({
email:'test'
});

// create mock listing with the user_id attached to property userId.
var listing = new listings({
userId:user._id
});

// save the created user and listing.
user.save();
listing.save();

// find ONE document in the collection 'listings'.
// populate the userId.
// execute the function and let mongoose call your callback.
listings.findOne().populate('userId').exec(function(err, doc)
{
// ensure there are no errors.
if(!err && doc)
{
// print the doc we found.
// should give userdoc inside userId property.
console.log(doc);
}
});

// clear collections.
//users.remove({});
//listings.remove({});

可运行链接:http://runnable.com/U24GoC9x3L9alfPc/output

关于javascript - 我似乎无法引用 2 个模式来填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23580108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com