最近我弄乱了我的mongodb服务器,但我修复了它,除了这个目录外,一切都很顺利。当我npm安装mongoose回到这个目录,它的工作和一切,但假设弹出的node_modolus文件夹不是
door_bells:~/workspace/Associations.1 $ npm install mongoose
mongoose@4.5.10 ../../node_modules/mongoose
├── sliced@1.0.1
├── ms@0.7.1
├── hooks-fixed@1.2.0
├── regexp-clone@0.0.1
├── async@1.5.2
├── muri@1.1.0
├── mpromise@0.5.5
├── kareem@1.1.3
├── mpath@0.2.1
├── bson@0.4.23
├── mquery@1.11.0 (debug@2.2.0, sliced@0.0.5, bluebird@2.10.2)
└── mongodb@2.1.18 (es6-promise@3.0.2, readable-stream@1.0.31, mongodb-core@1.3.18)
当我尝试在这个目录中运行一个js文件时,这是我得到的错误:
door_bells:~/workspace/Associations $ node embed.js
events.js:141
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'posts' of null
at /home/ubuntu/workspace/Associations/embed.js:56:12
at Query.<anonymous> (/home/ubuntu/node_modules/mongoose/lib/model.js:3327:16)
at /home/ubuntu/node_modules/mongoose/node_modules/kareem/index.js:259:21
at /home/ubuntu/node_modules/mongoose/node_modules/kareem/index.js:127:16
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)
var userSchema = new mongoose.Schema({
email: String,
name: String,
posts: [postSchema]
});
var User = mongoose.model('User', userSchema);
User.findOne({name: 'sl'}, function(err,user){
if(err) {
console.log(err);
} else {
user.posts.push({
title: '3 Things i really hate',
content: 'uuK,SyY,x'
});
user.save(function(err,user){ //user save
if(err){
console.log(err);
} else {
console.log(user);
}
});
}
});
答案 0 :(得分:0)
如果你有一两个问题,你的问题有点不清楚。对于&#39;帖子&#39;问题可能是因为您没有找到匹配的用户。你应该检查一下:
User.findOne({name: 'sl'}, function(err,user){
if(err) {
console.log(err);
} else {
// There is no reason to assume 'user' exists here. Checking for errors would not show this.
user.posts.push({
因此...
if(user) {
user.posts.push({
关于node_modules
的问题。如果你确实有问题,那就不完全清楚了。有些事情确实搞砸了,在这种情况下,我会删除整个node_modules
文件夹并重新创建它。
rm -rf node_modules // be sure to be in you project directory - or delete by hand if you prefer
npm i // Reinstall it all