Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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 当字段不是';图式中的t_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js 当字段不是';图式中的t

Node.js 当字段不是';图式中的t,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有一个模式Dog,strict设置为false,允许保存模式中没有的值。但是,我似乎不能使用点符号来设置如下值: var dog = new Dog(); dog.name = "Barry"; // works because name is in schema dog.notInSchema = "This should work!" // doesn't work because its not in the schema dog.save(...) 像这样的方法确实有效: var d

我有一个模式
Dog
,strict设置为false,允许保存模式中没有的值。但是,我似乎不能使用点符号来设置如下值:

var dog = new Dog();
dog.name = "Barry"; // works because name is in schema
dog.notInSchema = "This should work!" // doesn't work because its not in the schema
dog.save(...)
像这样的方法确实有效:

var dog = new Dog({name : "kyle", notInSChema : "This works and saves" })
dog.save(...)

我需要在这里对架构中不存在的字段使用点表示法,如何才能做到这一点?

在Mongoose模型上设置项的最佳方法是使用model#set。在您的示例中,这将是:

var dog = new Dog();
Dog.set('name', 'Barry');
Dog.set('notInSchema', 'this does work');
dog.save(callback);