gpt4 book ai didi

node.js - nodejs/ Mongoose : What is wrong with my chained . then() 调用

转载 作者:可可西里 更新时间:2023-11-01 10:39:24 25 4
gpt4 key购买 nike

我下面的代码试图:

  1. 创建User 模型的实例
  2. Subscriber 模型中找到与新创建的用户具有相同电子邮件地址的实例
  3. 将新用户的 subscribedAccount 属性与 findOneuser.email 查询找到的 Subscriber 实例相关联>

代码:

// Check that I have a subscriber with email 'test@test.com'
Subscriber.findOne({email:'test@test.com'})
.then(d => console.log(`\nResult of check for a subscriber with email test@test.com:\n ${d}`));

User.create({name: {first: 'test first', last: 'test last'}, email: 'test@test.com', password: 'pass123'})
.then(u => {
user = u;
// Check that user.email contains 'test@test.com'
console.log(`\nCreated user's email address: ${user.email}\n`);
Subscriber.findOne({email: user.email});
})
.then(s => {
console.log(`\nAnything found by findOne and passed to this .then()?: ${s}`);
user.subscribedAccount = s;
user.save();
})
.catch(e => console.log(e.message));

控制台结果:

Server running at http://localhost:3000 Successfully connected with Mongoose!

Result of check for a subscriber with email test@test.com:
{ groups: [], _id: 5aa422736518f30fbc0f77e2, name: 'test name', email: 'test@test.com', zipCode: 11111, __v: 0 }

Created user's email address: test@test.com

Anything found by findOne and passed to this .then()?: undefined

为什么 Subscriber.findOne 返回 undefined?这是实际发生的事情还是我遗漏的其他事情?

这是我对 UserSubscriber 的模型定义。如果您需要从应用程序中查看任何其他内容以了解发生了什么,请告诉我。

用户:

const mongoose = require('mongoose');
const {Schema} = require('mongoose');

var userSchema = new Schema( {
name: {
first: {
type: String,
trim: true
},
last: {
type: String,
trim: true
}
},
email: {
type: String,
required: true,
lowercase: true,
unique: true
},
zipCode: {
type: Number,
min: [ 10000, 'Zip code too short' ],
max: 99999
},
password: {
type: String,
required: true
},
courses: [ {
type: Schema.Types.ObjectId,
ref: 'Course'
} ],
subscribedAccount: {
type: Schema.Types.ObjectId,
ref: 'Subscriber'
}
}, {
timestamps: true
} );

userSchema.virtual('fullName').get(function() {
return `${this.name.first} ${this.name.last}`;

});
module.exports = mongoose.model('User', userSchema);

订阅者:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let subscriberSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
lowercase: true,
unique: true
},
zipCode: {
type: Number,
min: [10000, 'Zip Code too short'],
max: 99999
},
groups: [{type: Schema.Types.ObjectId, ref: 'Group'}]
});

subscriberSchema.methods.getInfo = function() {
return `Name: ${this.name} Email: ${this.email} Zip Code: ${this.zipCode}`;
}
subscriberSchema.methods.findLocalSubscribers = function() {
return this.model('Subscriber')
.find({zipCode: this.zipCode})
.exec();
}

//model.exports = mongoose.model('Subcriber', subscriberSchema);

var Subscriber = exports.Subscriber = mongoose.model('Subscriber', subscriberSchema);

最佳答案

你应该这样做

// Check that I have a subscriber with email 'test@test.com'
Subscriber.findOne({email:'test@test.com'})
.then(d => console.log(`\nResult of check for a subscriber with email test@test.com:\n ${d}`));

User.create({name: {first: 'test first', last: 'test last'}, email: 'test@test.com', password: 'pass123'})
.then(u => {
user = u;
// Check that user.email contains 'test@test.com'
console.log(`\nCreated user's email address: ${user.email}\n`);
Subscriber.findOne({email: user.email});


console.log(`\nAnything found by findOne and passed to this .then()?: ${s}`);
user.subscribedAccount = s;
user.save()
.then(s => {
//user has been updated
})
.catch(err => {
res.status(err).json(err);
})

})
})
. catch(e => console.log(e.message));

关于node.js - nodejs/ Mongoose : What is wrong with my chained . then() 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212734/

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