Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我无法使用node js将数据保存到mongodb john.save(函数(err){ ^ TypeError:无法读取未定义的属性“save” at对象。(C:\Users\shreeranga\Documents\mongoconnect\mongo1.js:18:6) 在模块处编译(Module.js:635:30) 在Object.Module.\u extensions..js(Module.js:646:10) 在Module.load(Module.js:554:32) 在tryModuleLoad时(module.js:497:12) 在Function.Module.\u加载(Module.js:489:3) 位于Function.Module.runMain(Module.js:676:10) 启动时(bootstrap_node.js:187:16) 在bootstrap_node.js:608:3[在此处输入图像描述][1]** **这是我的密码** var MongoClient=require('mongodb')。MongoClient,format=require('util')。format; var mongoose=require('mongoose'); mongoose.Promise=require('bluebird'); var mongodb=require('mongodb'); MongoClient.connect('mongodb://127.0.0.1:27017/mydatabase'); var Schema=mongoose.Schema; var personSchema=新模式({ 名字:String, 姓氏:String, 地址:String }); 变量Person=mongoose.model('Person',personSchema); var john=人({ 名字:“约翰”, 姓氏:“Doe”, 地址:缅因街555号 }); john.save(函数(err){ 如果(错误)抛出错误; console.log('person saved!'); });_Node.js_Mongodb_Mongoose_Mongoose Schema - Fatal编程技术网

我无法使用node js将数据保存到mongodb john.save(函数(err){ ^ TypeError:无法读取未定义的属性“save” at对象。(C:\Users\shreeranga\Documents\mongoconnect\mongo1.js:18:6) 在模块处编译(Module.js:635:30) 在Object.Module.\u extensions..js(Module.js:646:10) 在Module.load(Module.js:554:32) 在tryModuleLoad时(module.js:497:12) 在Function.Module.\u加载(Module.js:489:3) 位于Function.Module.runMain(Module.js:676:10) 启动时(bootstrap_node.js:187:16) 在bootstrap_node.js:608:3[在此处输入图像描述][1]** **这是我的密码** var MongoClient=require('mongodb')。MongoClient,format=require('util')。format; var mongoose=require('mongoose'); mongoose.Promise=require('bluebird'); var mongodb=require('mongodb'); MongoClient.connect('mongodb://127.0.0.1:27017/mydatabase'); var Schema=mongoose.Schema; var personSchema=新模式({ 名字:String, 姓氏:String, 地址:String }); 变量Person=mongoose.model('Person',personSchema); var john=人({ 名字:“约翰”, 姓氏:“Doe”, 地址:缅因街555号 }); john.save(函数(err){ 如果(错误)抛出错误; console.log('person saved!'); });

我无法使用node js将数据保存到mongodb john.save(函数(err){ ^ TypeError:无法读取未定义的属性“save” at对象。(C:\Users\shreeranga\Documents\mongoconnect\mongo1.js:18:6) 在模块处编译(Module.js:635:30) 在Object.Module.\u extensions..js(Module.js:646:10) 在Module.load(Module.js:554:32) 在tryModuleLoad时(module.js:497:12) 在Function.Module.\u加载(Module.js:489:3) 位于Function.Module.runMain(Module.js:676:10) 启动时(bootstrap_node.js:187:16) 在bootstrap_node.js:608:3[在此处输入图像描述][1]** **这是我的密码** var MongoClient=require('mongodb')。MongoClient,format=require('util')。format; var mongoose=require('mongoose'); mongoose.Promise=require('bluebird'); var mongodb=require('mongodb'); MongoClient.connect('mongodb://127.0.0.1:27017/mydatabase'); var Schema=mongoose.Schema; var personSchema=新模式({ 名字:String, 姓氏:String, 地址:String }); 变量Person=mongoose.model('Person',personSchema); var john=人({ 名字:“约翰”, 姓氏:“Doe”, 地址:缅因街555号 }); john.save(函数(err){ 如果(错误)抛出错误; console.log('person saved!'); });,node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,您的代码: john.save(function(err) { ^ TypeError: Cannot read property 'save' of undefined at Object.<anonymous> (C:\Users\shreeranga\Documents\mongoconnect\mongo1.js:18:6) at Module._compile (module.js:635:30) at Object.Module._ex

您的代码:

john.save(function(err) {
     ^

TypeError: Cannot read property 'save' of undefined
    at Object.<anonymous> (C:\Users\shreeranga\Documents\mongoconnect\mongo1.js:18:6)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3[enter image description here][1]**
**here is my code **
        var MongoClient = require('mongodb').MongoClient, format = require('util').format;
    var mongoose = require('mongoose');
    mongoose.Promise = require('bluebird');
    var mongodb=require('mongodb');
    MongoClient.connect('mongodb://127.0.0.1:27017/mydatabase');
    var Schema = mongoose.Schema;
    var personSchema = new Schema({
        firstname: String,
        lastname: String,
        address: String
    });
    var Person = mongoose.model('Person', personSchema);
    var john = Person({
        firstname: 'John',
        lastname: 'Doe',
        address: '555 Main St.'
    });
    john.save(function(err) {
        if (err) throw err;
        console.log('person saved!');
    });
您需要使用
new
来实例化mongoose模型文档:

var Person = mongoose.model('Person', personSchema);
var john = Person({
    firstname: 'John',
    lastname: 'Doe',
    address: '555 Main St.'
});
john.save(function(err) {
    if (err) throw err;
    console.log('person saved!');
});

然后你的
john.save()
将起作用

你能添加一些与你的错误相关的代码片段吗?例如,如果你的
john
对象存在,请不要发布代码图片,请按照帮助中心提供的说明在此处包含所有相关代码。//这是源代码我已编辑了问题,请查看var Person=mongoose.model('Person',personSchema);var john=new Person({firstname:'john',lastname:'Doe',address:'555 Main St.});john.save(函数(err){if(err)throw err;console.log('Person saved!');});//但是我仍然无法在控制台上获取消息控制台上没有打印消息
var john = new Person({})